如何在屏幕之间保留navigationItem.titleView?

时间:2017-03-18 23:56:37

标签: ios uinavigationcontroller navigationbar uinavigationitem

我正在使用NavigationController来管理三个屏幕。所有三个屏幕共享一个共同的图像作为标题。我在每个VC的viewWillAppear中设置图像,如下所示:

self.navigationItem.titleView = myImageView

问题是每当按下/弹出一个屏幕,导航动画并且新图像将来自右侧或左侧。我希望图像能够持续存在并且保持在中间没有动画。

有没有办法禁用这个动画?

2 个答案:

答案 0 :(得分:0)

只有在viewWillAppear中放置/设置titleView代码时,它才是可重现的。它正在移动,因为viewWillAppear在第四次(推送)期间调用了后退(弹出)两个操作。将其设置为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

答案 1 :(得分:0)

另一个困难但可行的解决方案是:

您需要使用 UINavigationController 委托方法来确定何时显示 UIViewController 。然后对于每个UIViewController,需要创建一个布尔变量,如 isInitialized 属性,它可以帮助您确定何时将UIViewController压入堆栈,或者何时将其显示在下一个视图控制器的后面。 / p>

您的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()
            }
        }
 }