为什么标签栏项目只显示其标题而没有图标?
Why tab bar items only display their titles without icons?
我创建了一个简单的 TabBarController 并将系统图像作为图标:
class TabBarController: UITabBarController {
private let profile = ProfileView()
private let explore = ExploreView()
override func viewDidLoad() {
super.viewDidLoad()
configure()
}
private func configure() {
explore.tabBarItem = UITabBarItem(
title: "Explore",
image: UIImage(named: "globe")?.withRenderingMode(.alwaysOriginal),
tag: 0
)
profile.tabBarItem = UITabBarItem(
title: "Profile",
image: UIImage(named: "person.fill")?.withRenderingMode(.alwaysOriginal),
tag: 1
)
self.viewControllers = [explore, profile]
self.selectedIndex = 0
}
}
但是当我 运行 项目时,我只看到没有图标的标题。有什么问题吗?我发现的其他问题是面向故事板的,但如何以编程方式解决它。
改用如下
explore.tabBarItem = UITabBarItem(
title: "Explore",
image: UIImage(systemName: "globe")?.withRenderingMode(.alwaysTemplate),
tag: 0
)
如果图片是系统使用的:
UIImage(systemName: "imageName")?.withRenderingMode(.alwaysTemplate)
我创建了一个简单的 TabBarController 并将系统图像作为图标:
class TabBarController: UITabBarController {
private let profile = ProfileView()
private let explore = ExploreView()
override func viewDidLoad() {
super.viewDidLoad()
configure()
}
private func configure() {
explore.tabBarItem = UITabBarItem(
title: "Explore",
image: UIImage(named: "globe")?.withRenderingMode(.alwaysOriginal),
tag: 0
)
profile.tabBarItem = UITabBarItem(
title: "Profile",
image: UIImage(named: "person.fill")?.withRenderingMode(.alwaysOriginal),
tag: 1
)
self.viewControllers = [explore, profile]
self.selectedIndex = 0
}
}
但是当我 运行 项目时,我只看到没有图标的标题。有什么问题吗?我发现的其他问题是面向故事板的,但如何以编程方式解决它。
改用如下
explore.tabBarItem = UITabBarItem(
title: "Explore",
image: UIImage(systemName: "globe")?.withRenderingMode(.alwaysTemplate),
tag: 0
)
如果图片是系统使用的:
UIImage(systemName: "imageName")?.withRenderingMode(.alwaysTemplate)