我使用rootViewController,我想移到另一个ViewController。到该代码的过渡到newViewController。
当newViewController嵌入在UINavigationController中时,会发生问题。然后,导航栏将在动画过程中进行动画处理并更改位置。
导航栏正在从左上角移动到正确的位置。
fileprivate func animateTransition(to newViewController: UIViewController) {
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
}, completion: nil)
}
如何将带有“淡入淡出”动画的另一个UINavigationController移到另一个位置,以及导航栏如何从动画开始就位于正确的位置?
答案 0 :(得分:0)
尝试将其放在newViewController的类声明下面
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
如果您的ViewController中已经有一个viewDidLoad(),则只需使用最后一部分。
如果这样不起作用,请告诉我。
答案 1 :(得分:0)
首先,您应该将视图控制器清理代码调用从animations
闭包移到completion
闭包:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
// this is intentionally blank
}, completion: { _ in
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
您不希望在动画制作完成之前完成过渡。
要解决的是导航栏问题,而不是让transition(from:to:duration:...)
处理视图控制器层次结构的操作,您可以将其添加到视图层次结构中,然后对其取消隐藏进行动画处理。通常,您会使用.showHideTransitionViews
选项,但是transition
仍会对外观方法感到好奇,这会混淆导航控制器,因此最好自己对其进行动画处理:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
newViewController.view.alpha = 0
view.addSubview(newViewController.view)
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseOut, animations: {
newViewController.view.alpha = 1
}, completion: { _ in
self.currentViewController.view.removeFromSuperview()
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
这将使它从一开始就正确显示导航栏,然后使其淡入。