工作表演示文稿Swift中iOS 12的控制器传统(问题)

nhjlsmyf  于 2022-12-30  发布在  iOS
关注(0)|答案(1)|浏览(132)

我不得不扩展对目标iOS 15的Cocoapod的支持,并将UINavigationController作为底层模态显示,用于UIViewController

问题是self.bottomController.sheetPresentationController仅适用于iOS 15,并且必须在目标12.0之前工作
如果我注解这行:

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

iOS 12目标编译成功,但模态内容显示为全屏。
如何模仿或做一些事情来显示内容(红色文字)作为模态,作为图片?
我试着使用其他豆荚到我的豆荚,但没有成功的结果。谢谢!

ymdaylpp

ymdaylpp1#

sheetPresentationController仅适用于ios 15及更高版本,对于以前版本,您需要设置。自定义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:-UI视图控制器转换委托

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

相关问题