简体   繁体   中英

Why tab bar items only display their titles without icons?

I created a simple TabBarController and put system images as icons:

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
    }
}

But when I run the project, I see only titles without icons. What's a problem? Other questions I found were storyboard oriented but how to fix it programmatically.

在此处输入图像描述

Use instead as below

演示

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

if the image is of the system use:

UIImage(systemName: "imageName")?.withRenderingMode(.alwaysTemplate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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