简体   繁体   English

以编程方式将系统图像添加到导航栏

[英]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.

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