简体   繁体   中英

titleView image appears and disappears immediately

I'm trying to add my image on UINavigationItem using this code:

override func viewDidLoad() {
        super.viewDidLoad()

        self.navItemTop.titleView?.contentMode = UIViewContentMode.scaleToFill

        self.navItemTop.titleView? = ViewController4.navigationImage! //my image, its correct for sure

        self.navigationController?.navigationBar.barTintColor = UIColor.white

    }

When i go to another UIViewController and then go back, image appears and disappears a second later. I've tried to put this piece of code in viewWillAppear() method, but result is the same. How can i fix this?

You need to set image in the viewWillAppear: method.

Reason: When you are push or Present Sometime there is possibility that we can set another image or clear color to Nav. Bar that's why viewDidLoad: method not called on back you lost your old settings of nav. Bar.

You can add it inside UIImageView:-

let image = UIImage(named: "yourimage.png")
let imageView = UIImageView(image:image)
self.navigationItem.titleView = imageView

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