Swift:如何在自定义segue之后保留嵌入式导航控制器?

时间:2016-02-22 15:55:58

标签: ios swift uinavigationcontroller uistoryboardsegue

我正在尝试从嵌入在导航控制器中的视图控制器设置自定义segue。当我使用show segue时,一切都很好,导航栏仍保持预期状态。但是,当我尝试使用自定义segue类时,导航栏会消失。检查后,导航控制器不在定制后的segue视图控制器中。所以似乎问题来自于使用自定义segue:如何使用自定义segue保持在导航控制器中?这是我的自定义segue代码,其目的是使segue从上到下具有幻灯片效果:

class FirstCustomSegue: UIStoryboardSegue {
override func perform() {
    var firstVCView = self.sourceViewController.view as UIView!
    var secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0.0, -screenHeight, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(1, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, screenHeight)
        secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, screenHeight)

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                animated: false,
                completion: nil)
    }

}

}

1 个答案:

答案 0 :(得分:1)

我认为对于自定义segue,您必须执行navigationController.pushViewController()操作以使新VC出现在旧NC堆栈上。我无法通过自定义segue找到实现这一目标的方法。但是,我确实看到了让它看起来像是这样的方法:

class FirstCustomSegue: UIStoryboardSegue {
override func perform() {
    let firstVCView = self.sourceViewController.view as UIView!
    let secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0.0, -screenHeight, screenWidth, screenHeight)

    // make a UIView which looks like the sourceViewController's view, 
    // and add it to the destinationViewcontrollers's view, 
    // in the right place so it looks as though it does not move
    let v1 = firstVCView.snapshotViewAfterScreenUpdates(false)
    v1.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight)
    secondVCView.addSubview(v1)

    // push the destination VC without animation
    // but it looks the same as the first so it seems as though nothing has happened.
    self.sourceViewController.navigationController?.pushViewController(self.destinationViewController, animated: false)
    // Animate the transition.
    UIView.animateWithDuration(1, animations: { () -> Void in
            secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, screenHeight)
        }) { (Finished) -> Void in
            v1.removeFromSuperview()
        }
    }
}

在没有动画的情况下执行pushViewController,然后通过将源视图的快照临时放到第二个视图上使其看起来像动画一样。它在我的iPad上看起来不错。