简体   繁体   English

从导航控制器到视图控制器的Swift自定义展开Segue

[英]Swift custom unwind Segue from Navigation controller to View Controller

I have 2 controllers: 我有2个控制器:

  • First view controller (1) 第一视图控制器(1)
  • Second view controller (2) embed in Navigation controller (2a) 嵌入在导航控制器(2a)中的第二视图控制器(2)

( 1 ) -> ( 2a - 2 ) (1)->(2a-2)

And custom Segue navigation between VC: 与VC之间的自定义Segue导航:

  • normal CustomSegue. 正常的CustomSegue。 From 1 to 2 从1到2
  • unwind CustomUnwindSegue. 展开CustomUnwindSegue。 From 2 to 1 从2到1

When I use normal segue: Second controller (including navigation bar!!!) push First controller from right side 当我使用常规segue时:第二个控制器(包括导航栏!!!)从右侧推第一个控制器

When I use unwind segue: First controller push Second controller (without navigation bar!!! Only view inside Navigation controller) to right side. 当我使用放松顺序时:第一个控制器向右推第二个控制器(没有导航栏!!!!! Navigation bar is sticky! 导航栏很粘! When navigation completed bar disappears. 导航完成后,栏消失。

How can I push View Controller with Navigation bar. 如何通过导航栏推送View Controller。

My code: 我的代码:

class CustomSegue: UIStoryboardSegue {

    override func perform() {

        let toViewController: UIViewController = self.destinationViewController as! UIViewController
        let fromViewController: UIViewController = self.sourceViewController as! UIViewController

        let containerView: UIView? = fromViewController.view.superview
        let screenBounds: CGRect = UIScreen.mainScreen().bounds

        let finalToFrame: CGRect = screenBounds
        let finalFromFrame: CGRect = CGRectOffset(finalToFrame, -screenBounds.size.width, 0)

        toViewController.view.frame = CGRectOffset(finalToFrame, screenBounds.size.width, 0)
        containerView?.addSubview(toViewController.view)

        UIView.animateWithDuration(0.5, animations: {

            toViewController.view.frame = finalToFrame
            fromViewController.view.frame = finalFromFrame

            }, completion: { finished in
                let fromVC: UIViewController = self.sourceViewController as! UIViewController
                let toVC: UIViewController = self.destinationViewController as! UIViewController
                fromVC.presentViewController(toVC, animated: false, completion: nil)
        })
    }
}

class CustomUnwindSegue: UIStoryboardSegue {

    override func perform() {

        let toViewController: UIViewController = self.destinationViewController as! UIViewController
        let fromViewController: UIViewController = self.sourceViewController as! UIViewController

        let containerView: UIView? = fromViewController.view.superview
        let screenBounds: CGRect = UIScreen.mainScreen().bounds

        let finalToFrame: CGRect = screenBounds
        let finalFromFrame: CGRect = CGRectOffset(finalToFrame, screenBounds.size.width, 0)

        toViewController.view.frame = CGRectOffset(finalToFrame, -screenBounds.size.width, 0)
        containerView?.addSubview(toViewController.view)

       UIView.animateWithDuration(0.5, animations: {

            toViewController.view.frame = finalToFrame
            fromViewController.view.frame = finalFromFrame

            }, completion: { finished in
                let fromVC: UIViewController = self.sourceViewController as! UIViewController
                let toVC: UIViewController = self.destinationViewController as! UIViewController
                fromVC.dismissViewControllerAnimated(false, completion: nil)
        })
    }
}

In the forward segue, you are animating from VC1 to VC2a, which will be the destination of the segue. 在正向序列中,您是从VC1动画到VC2a,这将是序列的目的地。 Animating VC2a in also makes VC2 appear, because VC2a presents it as part of its view. 对VC2a进行动画处理也会使VC2出现,因为VC2a将其显示为视图的一部分。 So the UINavigationBar and VC2 both slide in. 因此,UINavigationBar和VC2都可以插入。

In the reverse segue, you are animating from VC2 to VC1. 在反向序列中,您是从VC2动画到VC1。 VC2's view does not cover the whole screen, because some is taken up by the UINavigationBar. VC2的view无法覆盖整个屏幕,因为UINavigationBar占据了其中的一部分。 You animate VC2's view to the side and replace it with VC1, and when that is finished, you dismissViewControllerAnimated which causes VC2a to disappear without animation. 您可以对VC2的视图进行动画处理,并将其替换为VC1,完成后,您可以dismissViewControllerAnimated ,这会使VC2a消失而没有动画。

Instead of animating VC2's view in the reverse segue, you need to animate VC2a's view again. 无需在反向序列中为VC2的视图设置动画,而是需要再次为VC2a的视图设置动画。 This should carry VC2 along with it. 这应该随带VC2。 I haven't tried this code, but something like this: 我没有尝试过这段代码,但是像这样:

   UIView.animateWithDuration(0.5, animations: {
        toViewController.view.frame = finalToFrame
        VC2a.view.frame = finalFromFrame
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM