[英]Adding System Image to navigation bar programmatically
I'm trying to move from storyboards to coding programmatically.我正在尝试从情节提要转向以编程方式进行编码。 I'm trying to add a barbuttonitem to my navigation bar.
我正在尝试将 barbuttonitem 添加到我的导航栏。 However, I don't want to use my own images, I want to use the default system images, such as "magnifyingglass.circle".
但是,我不想使用自己的图像,我想使用默认的系统图像,例如“magnifyingglass.circle”。
Here is my code:这是我的代码:
let button = UIBarButtonItem.init(image: UIImage(contentsOfFile: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = button
However, nothing is added to my navigation bar.但是,我的导航栏中没有添加任何内容。
You are using the wrong UIImage
initializer.您使用了错误的
UIImage
初始化程序。 The one you are trying to use requires a full path to an image file.您尝试使用的那个需要图像文件的完整路径。 You want
UIImage(systemName:)
.你想要
UIImage(systemName:)
。
let button = UIBarButtonItem(image: UIImage(systemName: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.