在iPhone +模型上查看无法正确显示

时间:2018-05-19 09:47:26

标签: ios swift presentmodalviewcontroller

在iPhone 7模拟器中,我有一个名为cartView的视图,看起来像......

enter image description here

按下底部cartView上的下一个按钮,会显示另一个视图......

enter image description here

此呈现的视图称为presentedView

按下cartView上的下一个按钮时写的代码就是这个......

let vc = PresentedUserDetailsViewController()
vc.modalPresentationStyle = .custom
present(vc, animated: true, completion: nil)

presentedView中,这些已在viewDidLoad ...

之前声明
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模型(比如iPhone 6 plus或iPhone 7 plus)中运行应用程序并按下cartView上的下一个按钮时,这就是我得到的......

enter image description here

在这里,我只是将presentedView着色以使其与众不同。在这种情况下,presentedView不仅不会填满整个屏幕尺寸,还会看到cartView的一部分,包括cartView的下一个按钮。

如何让presentedView正确显示在iPhone 7型号中的加型iPhone型号上(从顶部开始的第二个屏幕截图)

编辑1:iPhone 6上的屏幕截图以及更改后的内容......

enter image description here

编辑:2如在iPad Air 2中所见......

enter image description here

3 个答案:

答案 0 :(得分:1)

在这种情况下,您需要在添加子视图时设置约束。

containerView.addSubview(toVC.view)

使用autolayout来设置约束

**更新:**

extension UIView{
     public func attachTo(view : UIView, animated : Bool = true){

            if(animated){
                self.alpha = 0
            }
            else{
                self.alpha = 1
            }

            view.addSubview(self)
            // self.frame = view.bounds

            self.translatesAutoresizingMaskIntoConstraints = false
            self.topAnchor.constraint(equalTo: self.superview!.topAnchor).isActive = true
            self.bottomAnchor.constraint(equalTo: self.superview!.bottomAnchor).isActive = true
            self.leadingAnchor.constraint(equalTo: self.superview!.leadingAnchor, constant: 0).isActive = true
            self.trailingAnchor.constraint(equalTo: self.superview!.trailingAnchor, constant: 0).isActive = true


            if(animated){
                UIView.animate(withDuration: 0.3, animations: {
                    self.alpha = 1
                })
            }
        }
}

现在,删除

  

containerView.addSubview(toVC.view)

并添加:

  

toVC.view.attachTo(view:containerView)

答案 1 :(得分:0)

我想也许你在let bdView = UIView(frame: self.view.bounds)得到了错误的框架。尝试使用autolayout

答案 2 :(得分:0)

containerView.addSubview(toVC.view)之后添加以下内容:

toVC.view.autoresizingMasks = [.flexibleWidth, .flexibleHeight]