[英]How to persist navigationItem.titleView between screen?
我正在使用NavigationController來管理三個屏幕。 所有三個屏幕共享一個共同的圖像作為標題。 我在每個VC的viewWillAppear中設置圖像如下:
self.navigationItem.titleView = myImageView
問題是每當按下/彈出屏幕,導航動畫並且新圖像將來自右側或左側時。 我希望圖像能夠持續存在並且保持在中間沒有動畫。
有沒有辦法禁用這個動畫?
只有在viewWillAppear
放置/設置titleView代碼時,它才是可重現的。 它正在移動,因為viewWillAppear在第四次(push)和后退(pop)兩種操作中都被調用。 將其設置為viewDidLoad
,它將解決此問題
override fun viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = myImageView
}
這個問題的另一個替代解決方案是
var isViewDidLoadCalled = false
override fun viewDidLoad() {
super.viewDidLoad()
isViewDidLoadCalled = true
}
override fun viewWillAppear(_ animated: Bool) {
super. viewWillAppear(animated)
if (isViewDidLoadCalled) {
self.navigationItem.titleView = myImageView
isViewDidLoadCalled = false
}
}
我建議使用viewDidLoad來設置你的titleView
另一個困難但可行的解決方案是
您需要使用UINavigationController委托方法來查明何時顯示UIViewController 。 然后對於每個UIViewController,需要創建一個類似於isInitialized屬性的布爾變量,它可以幫助您確定何時將UIViewController壓入堆棧,或者何時將其顯示在下一個視圖控制器的后面。
你的FirstViewController:
func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
var navigationBarAnimation = CATransition()
navigationBarAnimation.duration = 1.5
navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
navigationBarAnimation.type = kCATransitionFade
navigationBarAnimation.subtype = kCATransitionFade
navigationBarAnimation.removedOnCompletion = true
self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
} else {
self.isInitialized = true;
}
}
}
func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
self.navigationController?.navigationBar?.layer?.removeAllAnimations()
}
}
}
你的SecondViewController:
func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if !self.isInitialized {
var navigationBarAnimation = CATransition()
navigationBarAnimation.duration = 1.5
navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
navigationBarAnimation.type = kCATransitionFade
navigationBarAnimation.subtype = kCATransitionFade
navigationBarAnimation.removedOnCompletion = true
self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
self.isInitialized = true;
}
}
}
func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
self.navigationController?.navigationBar?.layer?.removeAllAnimations()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.