为什么选项卡栏项仅显示其标题而不显示图标?

时间:2020-07-17 15:21:11

标签: ios swift uitabbarcontroller

我创建了一个简单的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
    }
}

但是当我运行项目时,我只会看到没有图标的标题。怎么了我发现的其他问题是针对情节提要的,但如何以编程方式解决。

enter image description here

2 个答案:

答案 0 :(得分:3)

使用如下代替

demo

    explore.tabBarItem = UITabBarItem(
        title: "Explore",
        image: UIImage(systemName: "globe")?.withRenderingMode(.alwaysTemplate),
        tag: 0
    )

答案 1 :(得分:1)

如果该图像属于系统,请使用:

UIImage(systemName:“ imageName”)?. withRenderingMode(.alwaysTemplate)