[英]How to change the item image of the Tab Bar Controller programmatically
I've manually added a Tab Bar Controller from Main file (XCode 14.1).我从主文件 (XCode 14.1) 中手动添加了一个标签栏 Controller。 I take it this is the Main storyboard
我认为这是主要的 storyboard
Both Item Scenes has their own controller class (custom class).两个项目场景都有自己的 controller class(自定义类)。
In either class, I want to change the item image and title.在任一 class 中,我想更改项目图像和标题。 For this question, the image.
对于这个问题,图片。 Inside one of the corresponding scene class (ProfileViewController):
对应场景class里面的其中一个(ProfileViewController):
override func viewDidLoad() {
super.viewDidLoad()
// Does nothing (both)
self.tabBarController?.tabBar.items![1].image = UIImage(named: "square.and.arrow.up.circle")
self.tabBarItem.image = UIImage(named: "square.and.arrow.up.circle")
}
I would like to change the image programatically as for one of the tabs, I get the image externally.我想以编程方式更改其中一个选项卡的图像,我从外部获取图像。
You are creating the image incorrectly.您正在错误地创建图像。 To use one of the SF Symbols you need to use:
要使用您需要使用的 SF 符号之一:
UIImage(systemName: "square.and.arrow.up.circle")
Using UImage(named:)
tries to load an image from your resource bundle or image assets.使用
UImage(named:)
尝试从您的资源包或图像资产中加载图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.