简体   繁体   中英

How to persist navigationItem.titleView between screen?

I am using NavigationController to manage three screens. All three screens share a common image as title. I set the image in the viewWillAppear in each VC as follow:

self.navigationItem.titleView = myImageView

The problem is whenever a screen is pushed/popped, the navigation is animated and a new image will come from right or left. I want the image to persist and remain in the middle none-animated.

Is there a way to disable this animation?

It's reproducible, only if you place/setting titleView code in viewWillAppear . it's moving because viewWillAppear called during forth(push) & back(pop) both operation. Set it into viewDidLoad , it will fix this issue

override fun viewDidLoad() {
 super.viewDidLoad()
 self.navigationItem.titleView = myImageView
}

One more alternate solution for this problem is

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
     }

 }

I recommend to use, viewDidLoad to setup your titleView

Another hard but feasible solution is:

You need to use the UINavigationController delegate methods to find out when the UIViewController is being shown. Then for each UIViewController, need to make a boolean variable like isInitialized property, which help you to determine when the UIViewController is being pushed on the stack, or when it's being shown upon back of next view controller.

Your 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()
            }
        }
    }

Your 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()
            }
        }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM