iPhone Plus机型上presentedView显示异常问题排查与解决
问题:iPhone Plus机型上自定义转场的presentedView无法填满屏幕
在iPhone 7模拟器中,我有一个名为cartView的视图,显示效果如下:
点击cartView底部的next按钮后,会弹出名为presentedView的视图,显示效果正常:
触发弹出的代码
let vc = PresentedUserDetailsViewController() vc.modalPresentationStyle = .custom present(vc, animated: true, completion: nil)
PresentedUserDetailsViewController核心实现
lazy var backdropView: UIView = { let bdView = UIView(frame: self.view.bounds) bdView.backgroundColor = UIColor.black.withAlphaComponent(0.5) return bdView }() let menuHeight = UIScreen.main.bounds.height / 2 var isPresenting = false init() { super.init(nibName: nil, bundle: nil) modalPresentationStyle = .custom transitioningDelegate = self }
自定义转场动画扩展
extension PresentedUserDetailsViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return self } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return self } func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 1 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerView = transitionContext.containerView let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) guard let toVC = toViewController else { return } isPresenting = !isPresenting if isPresenting == true { containerView.addSubview(toVC.view) menuView.frame.origin.y += menuHeight backdropView.alpha = 0 UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: { self.menuView.frame.origin.y -= self.menuHeight self.backdropView.alpha = 1 }, completion: { (finished) in transitionContext.completeTransition(true) }) } else { UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: { self.menuView.frame.origin.y += self.menuHeight self.backdropView.alpha = 0 }, completion: { (finished) in transitionContext.completeTransition(true) }) } } }
异常情况说明
但在iPhone Plus机型(如iPhone 6 Plus、iPhone 7 Plus)中运行时,presentedView显示异常:
此时presentedView无法填满整个屏幕,且背后的cartView(包括next按钮)仍可见。
修改后的预期显示效果(iPhone 6 Plus):
iPad Air 2上的显示效果:
请问如何让presentedView在iPhone Plus机型上像iPhone 7那样正常显示?
内容的提问来源于stack exchange,提问作者user8653398
相关产品推荐
相关产品推荐

