Swift自定义展开从导航控制器到View Controller的Segue

时间:2015-04-16 07:33:16

标签: ios swift uiviewcontroller uinavigationcontroller uinavigationbar

我有2个控制器:

  • 第一视图控制器(1)
  • 第二视图控制器(2)嵌入导航控制器(2a)

(1) - > (2a - 2)

VC之间定制Segue导航:

  • 正常的CustomSegue。从1到2
  • 展开CustomUnwindSegue。从2到1

当我使用普通segue时: 第二个控制器(包括导航栏!!!)从右侧推送第一个控制器

当我使用unwind segue时: 第一个控制器推动第二个控制器(没有导航栏!!!仅在导航控制器内查看)到右侧。导航栏很粘!当导航完成栏消失时。

如何使用导航栏推送View Controller。

我的代码:

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

1 个答案:

答案 0 :(得分:0)

在前向segue中,您将动画从VC1到VC2a,它将成为segue的目标。动画VC2a也会使VC2出现,因为VC2a将其作为其视图的一部分。所以UINavigationBar和VC2都会滑入。

在反向segue中,您将动画从VC2到VC1。 VC2的view没有涵盖整个屏幕,因为有些是由UINavigationBar占用的。您将VC2的视图设置为侧面并将其替换为VC1,当完成后,您dismissViewControllerAnimated会导致VC2a在没有动画的情况下消失。

您需要再次为VC2a的视图设置动画,而不是在反向segue中设置VC2的视图动画。这应该随身携带VC2。我没有尝试过这段代码,但是这样的话:

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