gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:48:34 25 4
gpt4 key购买 nike

我正在尝试从嵌入在导航 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com