在segue期间,如何将UIView保留在segue动画之上?

时间:2017-09-06 17:35:27

标签: ios swift

我有一个故事板segue,在显示新视图时,我希望UIView始终保持最佳状态,因此segue不会影响它。尝试了动画insertSubview,但它没有来自底部动画的推动。

1 个答案:

答案 0 :(得分:1)

这是一些代码,我很快就展示了两个视图控制器,你要去的那个和一个来自。我将动画放在一个名为buttonPressed的函数中,但它应该调用任何调用转换的函数。其余代码非常自我解释。

为此,两个视图控制器都需要一个具有相同名称(或不同名称但跟踪哪个是哪个)的视图,我使用IBOutlet staticView。然后在界面构建器中确保它们具有相同的约束或框架,以便当您将vc的静态视图设置为另一个时,它会粘贴到相同的位置。

class ViewControllerFrom: UIViewController {

        @IBOutlet weak var staticView: UIView!

        @IBAction func buttonPressed(_ sender: Any) {

            let toVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "toVC") as! ViewControllerTo

            //Add nextVC's view to ours as subview
            self.view.addSubview(toVC.view)

            //Set its starting height to be below current view.
            toVC.view.frame = CGRect(x: 0, y: view.frame.height, width: view.frame.width, height: view.frame.height)

            //This is important to make sure staticView stays in front of view animating in
            view.bringSubview(toFront: staticView)
            if toVC.staticView != nil {
                toVC.staticView.isHidden = true
            }
            //I use animation duration 0.4, close to default animations by iOS.
            UIView.animate(withDuration: 0.4, animations: {
                toVC.view.frame.origin.y = 0
            }, completion: { (success) in
                if success {
                    //Now that view is in place, set static view from old vc to new vc and reshow it. Then do the actual presentation unanimated.
                    toVC.staticView.isHidden = false
                    toVC.staticView = self.staticView
                    self.present(toVC, animated: false, completion: nil)
                }
            })
        }

    }

    class ViewControllerTo : UIViewController {

        @IBOutlet weak var staticView: UIView!

    }