xcode 如何在导航栏上使用图像徽标而不是标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39739660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:21:49  来源:igfitidea点击:

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

iosswiftxcodestoryboardswift3

提问by Mert Alnuaimi

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

我什至试图在课程中包含 IB 本身也不起作用,它似乎不是那样工作

@IBOutlet weak var navBar: UINavigationItem!

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

附注。我的标志是一个 200x40px 的 png,它在资产中命名为 logo.png。

My storyboard

我的故事板

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

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.

编辑:我通过在那里放置一个图像视图而不是整个导航项故事来解决我的问题。谢谢你的建议。

回答by Anbu.Karthik

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 

回答by Ketan Parmar

Set your imageview's content mode like,

设置您的图像视图的内容模式,例如,

 imageView.contentMode = .ScaleAspectFit

回答by Yerkebulan Abildin

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

这段代码看起来不错,我认为 UIImage 的名称有误,您是否调试并检查了let logo不是nil?尝试删除“.jpg”