Swift: 如何在自定义转场后保留嵌入式导航控制器?
Swift: how can I keep an embedded navigation controller after a custom segue?
我正在尝试从嵌入在导航控制器中的视图控制器设置自定义转场。当我使用 show segue 时,一切都很好,导航栏保持预期。但是,当我尝试使用自定义 segue class 时,导航栏消失了。检查后,导航控制器不存在于 post-自定义 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)
}
}
}
我认为对于自定义 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.
上看起来不错
我正在尝试从嵌入在导航控制器中的视图控制器设置自定义转场。当我使用 show segue 时,一切都很好,导航栏保持预期。但是,当我尝试使用自定义 segue class 时,导航栏消失了。检查后,导航控制器不存在于 post-自定义 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)
}
}
}
我认为对于自定义 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.