How to use an image logo on the navigation bar instead of title

二次信任 提交于 2020-01-03 16:52:35

问题


I have tried a couple different codes but couldn't make the logo show

import UIKit 
class HomeViewController: UIViewController { 

override func viewDidLoad() { 
super.viewDidLoad() 

let logo = UIImage(named: "logo.png") 
let imageView = UIImageView(image: logo) 
self.navigationItem.titleView = imageView
navigationItem.titleView?.sizeToFit() 
 } 
}

I even tried to include IB in the class itself didn't work either, it doesn't seem to work that way

@IBOutlet weak var navBar: UINavigationItem!

PS. My logo is a 200x40px png and its named logo.png in the assets.

My storyboard

http://i68.tinypic.com/b68t8o.png

Any help is appreciated

Edit: I solved my problem by putting an image view there instead of this whole navigation item story. Thanks for your suggestions though.


回答1:


try this

 let logo = UIImage(named: "logo.png") 
 let imageView = UIImageView(image: logo) 
 imageView.contentMode = .ScaleAspectFit // set imageview's content mode
 self.navigationItem.titleView = imageView 



回答2:


Set your imageview's content mode like,

 imageView.contentMode = .ScaleAspectFit



回答3:


This code looks good, i think there is error in name of UIImage, did you debug and check that let logo is not nil? try to remove ".jpg"



来源:https://stackoverflow.com/questions/39739660/how-to-use-an-image-logo-on-the-navigation-bar-instead-of-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!