【问题标题】:sheetPresentationController legacy for iOS 12 in Swift (Question)Swift 中 iOS 12 的 sheetPresentationController legacy(问题)
【发布时间】:2022-12-29 18:10:13
【问题描述】:

我必须扩展对为目标 iOS 15 制作的 Cocoapod 的支持,并且将 UINavigationController 显示为底部模式,为 UIViewController 呈现

问题是 self.bottomController.sheetPresentationController 仅适用于 iOS 15,并且必须在目标 12.0 之前有效

如果我评论这一行:

if let sheet = self.bottomController.sheetPresentationController {
    sheet.detents = [.large()]
}

并为 iOS 12 目标编译成功,但模态的内容显示为全屏。

如何模仿或做一些事情来将内容(红色文本)显示为模态,如图所示?

我尝试在我的 Pod 中使用其他 Pod,但没有成功。谢谢!

【问题讨论】:

    标签: ios swift uiviewcontroller legacy


    【解决方案1】:

    sheetPresentationController 仅适用于 ios 15 及更高版本,对于以前的版本,您需要设置 .custom modalPresentationStyle

                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)
        }
    }
    

    并按给定添加演示控制器

    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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-29
      • 1970-01-01
      • 2019-03-08
      • 2023-03-29
      • 2021-02-11
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多