0
votes

I'm having a problem with the titleView in navigation bar in iOS. I've created a custom view for the titleView, but every time I push another controller, the titleView immediately appear and disappear when I go back to the first view controller. Here's my code.

 override func viewDidLoad() {
            super.viewDidLoad()
    let logo = UIImage(named: "img_appbar_logo")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
                logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 40))
                logoContainer.backgroundColor = UIColor.clear
    
                animatedLogo = UIImageView(frame:  CGRect(x: 0, y: 0, width: logoContainer.frame.width, height: logoContainer.frame.height))
                animatedLogo.contentMode = .scaleAspectFit
                animatedLogo.clipsToBounds = true
                animatedLogo.image = logo
                
                logoContainer.addSubview(animatedLogo)
       
                navigationItem.titleView = logoContainer
    }
2
Did you check the navigationItem for the new controller you are pushing? I mean if the new controller shouldn’t have one, try setting it to nil or background color of item set to clear. Is this navigation item of the new controller is even coming into picture? - Teju Amirthi
Yeah, actually, i've also tried applying the logo on the new controller on viewDidLoad and even on viewWillAppear, unfortunately, the bug still occurring. - vidalbenjoe
I suggest to override the navigationItem property, see developer.apple.com/documentation/uikit/uiviewcontroller/… - Andreas Oetjen
Can you share a screen capture or a gif kind of visual stuff for better understanding about the issue @vidalbenjoe - Teju Amirthi

2 Answers

1
votes

I've already fixed the issue related to titleView in Navigation bar. I found out that after I push another controller and pop back, the titleView will be replaced by the view of empty UILabel in the NavigationItem. That's why the titleView appear then disappear.

How I fixed the issue?

I added the custom view directly to the navigation bar. Here's the code

 let logo = UIImage(named: "img_appbar_logo")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
                        logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 40))
    
                        logoContainer.backgroundColor = UIColor.red
        
                        animatedLogo = UIImageView(frame:  CGRect(x: 0, y: logoContainer.frame.origin.y, width: logoContainer.frame.width, height: logoContainer.frame.height))
                        animatedLogo.contentMode = .scaleAspectFit
                        animatedLogo.clipsToBounds = true
                        animatedLogo.image = logo
                
                        logoContainer.addSubview(animatedLogo)
                
                
                navigationController?.navigationBar.addSubview(logoContainer) <--- new
//navigationItem.titleView = logoContainer <---- old

Then remove the view on viewWillDisappear

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        logoContainer.removeFromSuperview()
    }
0
votes

Set the titleView in viewWillAppear since the titleView gets replaced by the title in the previous controller