【问题标题】:Swift: how can I keep an embedded navigation controller after a custom segue?Swift:如何在自定义 segue 之后保留嵌入式导航控制器?
【发布时间】:2016-06-04 03:23:10
【问题描述】:

我正在尝试从嵌入在导航控制器中的视图控制器设置自定义转场。当我使用 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)
    }

}

}

【问题讨论】:

标签: ios swift uinavigationcontroller uistoryboardsegue


【解决方案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 上看起来不错。

【讨论】:

  • 有趣的问题,我不确定我是否理解正确,最好的排序!感谢您的支持!
猜你喜欢
  • 1970-01-01
  • 2015-06-22
  • 2021-10-03
  • 2020-04-24
  • 1970-01-01
  • 2017-07-07
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多