ios - Swift:如何在自定义 segue 之后保留嵌入式导航 Controller ?

标签 ios swift uinavigationcontroller uistoryboardsegue

我正在尝试从嵌入在导航 Controller 中的 View Controller 设置自定义转场。当我使用 show segue 时,一切都很好,导航栏保持预期。但是,当我尝试使用自定义 segue 类时,导航栏消失了。检查后,自定义 segue View Controller 中不存在导航 Controller 。所以问题似乎来自使用自定义转场:如何使用自定义转场留在导航 Controller 中?下面是我自定义的转场代码,目的是让转场有从上到下的滑动效果:

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

}

最佳答案

我认为对于自定义 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 没有动画,然后通过将源 View 的快照临时放到第二个 View 上让它看起来像有动画。在我的 iPad 上看起来不错。

关于ios - Swift:如何在自定义 segue 之后保留嵌入式导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35558075/

相关文章:

ios - Xcode 游戏崩溃并指向 SpriteNode 说 "EXC_BAD_ACCESS(code=1, address=0xa001800"

objective-c - 容器 View Controller 示例

iphone - 如何控制 UINavigationBar 标题中的标题字体

ios - 如何自行调整单元格大小?

ios7 中的 swift3.0 崩溃

iphone - 以编程方式选择后,UITableView 中的 UITabBar MoreViewController 图像消失

ios - 关闭 Controller 后隐藏导航栏

ios - Objective-C Game Center自动匹配“由于与服务器通信出错,无法完成请求的操作。”

IOS 应用内购买 - 一旦用户购买了任何应用内购买项目,我们如何才能让他们访问所有应用内购买内容?

swift - 在后台收到通知时 didReceiveRemoteNotification 不起作用