skip to Main Content

I am learning swift and trying to pass a variable to a sheet but it isn’t changing the variable when the sheet pops up, is there a different way to do this?

let bottomSheet = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "bottomSheet")
    
    if let sheet = bottomSheet.sheetPresentationController{
        sheet.detents = [.medium()]
        sheet.prefersGrabberVisible = true
        sheet.preferredCornerRadius = 24
        sheet.prefersGrabberVisible = true
    }

    bottomSheet.isEnabled = true
    self.present(bottomSheet, animated: true, completion: nil)

And in the bottomSheet view controller I have the variable

var isEnabled: Bool = false

But even though I put true it always shows as false

2

Answers


  1. Try it like this, what you need to do is specify it as a view controller.

    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    if let bottomSheet = mainStoryboard.instantiateViewController(withIdentifier: "bottomSheet") as? BottomSheetVC{
            if let sheet = bottomSheet.sheetPresentationController{
                sheet.detents = [.medium()]
                sheet.prefersGrabberVisible = true
                sheet.preferredCornerRadius = 24
                sheet.prefersGrabberVisible = true
            }
            
            bottomSheet.isEnabled = true
            self.present(bottomSheet, animated: true)
        }
    
    Login or Signup to reply.
  2. sheetPresentationController only work for ios 15 and later, for previously versions you need to set .custom modalPresentationStype

            controller.modalPresentationStyle = .pageSheet
            if #available(iOS 15.0, *) {
    
                if let sheet = controller.sheetPresentationController {
                    sheet.detents = [.medium()]
                }
                
            } else {
                
                controller.modalPresentationStyle = .custom
                controller.transitioningDelegate = self
            }
            self.present(controller, animated: true, completion: nil)
    

    // MARK: – UIViewControllerTransitioningDelegate

    extension CPPdfPreviewVC: UIViewControllerTransitioningDelegate {
    
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        PresentationController(presentedViewController: presented, presenting: presenting)
        }
    

    and Add Presentation controller as given

    class PresentationController: UIPresentationController {
    
      let blurEffectView: UIVisualEffectView!
      var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
      
      override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
          let blurEffect = UIBlurEffect(style: .dark)
          blurEffectView = UIVisualEffectView(effect: blurEffect)
          super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
          tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissController))
          blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
          self.blurEffectView.isUserInteractionEnabled = true
          self.blurEffectView.addGestureRecognizer(tapGestureRecognizer)
      }
      
      override var frameOfPresentedViewInContainerView: CGRect {
          CGRect(origin: CGPoint(x: 0, y: self.containerView!.frame.height * 0.4),
                 size: CGSize(width: self.containerView!.frame.width, height: self.containerView!.frame.height *
                  0.6))
      }
    
      override func presentationTransitionWillBegin() {
          self.blurEffectView.alpha = 0
          self.containerView?.addSubview(blurEffectView)
          self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in
              self.blurEffectView.alpha = 0.7
          }, completion: { (UIViewControllerTransitionCoordinatorContext) in })
      }
      
      override func dismissalTransitionWillBegin() {
          self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in
              self.blurEffectView.alpha = 0
          }, completion: { (UIViewControllerTransitionCoordinatorContext) in
              self.blurEffectView.removeFromSuperview()
          })
      }
      
      override func containerViewWillLayoutSubviews() {
          super.containerViewWillLayoutSubviews()
        presentedView!.roundCorners([.topLeft, .topRight], radius: 22)
      }
    
      override func containerViewDidLayoutSubviews() {
          super.containerViewDidLayoutSubviews()
          presentedView?.frame = frameOfPresentedViewInContainerView
          blurEffectView.frame = containerView!.bounds
      }
    
      @objc func dismissController(){
          self.presentedViewController.dismiss(animated: true, completion: nil)
      }
    }
    
    extension UIView {
      func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
          let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners,
                                  cornerRadii: CGSize(width: radius, height: radius))
          let mask = CAShapeLayer()
          mask.path = path.cgPath
          layer.mask = mask
      }
    }
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search