简体   繁体   English

titleView图像立即出现并消失

[英]titleView image appears and disappears immediately

I'm trying to add my image on UINavigationItem using this code: 我正在尝试使用以下代码在UINavigationItem上添加我的图像:

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. 当我去另一个UIViewController然后返回时,图像出现并在一秒后消失。 I've tried to put this piece of code in viewWillAppear() method, but result is the same. 我试图将这段代码放在viewWillAppear()方法中,但结果是一样的。 How can i fix this? 我怎样才能解决这个问题?

You need to set image in the viewWillAppear: method. 您需要在viewWillAppear:方法中设置图像。

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. 这就是为什么viewDidLoad:方法没有被调用的原因你丢失了旧的nav设置。 Bar. 酒吧。

You can add it inside UIImageView:- 你可以在UIImageView中添加它: -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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