maksonnie
maksonnie

Reputation: 743

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.

enter image description here

Upvotes: 1

Views: 133

Answers (2)

Estudante BR
Estudante BR

Reputation: 11

if the image is of the system use:

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

Upvotes: 1

Asperi
Asperi

Reputation: 258413

Use instead as below

demo

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

Upvotes: 3

Related Questions