Home > database >  How to put custom colorful image in UITabBar
How to put custom colorful image in UITabBar

Time:12-26

I want to add custom image to the UITabBar, but it looses it's color, I guess it's because of the tintColor. What I get

What I want

class TabBarViewController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    UITabBar.appearance().backgroundColor = .menuColor
    UITabBar.appearance().tintColor = .white
    UITabBar.appearance().unselectedItemTintColor = .gray
    
    setupVCs()
}

private func createNavController(for rootViewController: UIViewController,
                                 title: String,
                                 image: UIImage,
                                 imageInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) -> UIViewController {
    let navController = UINavigationController(rootViewController: rootViewController)
    navController.tabBarItem.title = title
    navController.tabBarItem.image = image
    navController.tabBarItem.imageInsets = imageInsets
    navController.navigationBar.prefersLargeTitles = true
    rootViewController.navigationItem.title = title
    return navController
}

private func setupVCs() {
    viewControllers = [
        createNavController(for: ViewController(), title: NSLocalizedString("notes", comment: ""), image: UIImage(named: "notes")!),
        createNavController(for: ViewController(), title: NSLocalizedString("", comment: ""), image: UIImage(named: "rec")!, imageInsets: UIEdgeInsets(top: -10, left: 0, bottom: 10, right: 0)),
        createNavController(for: ViewController(), title: NSLocalizedString("settings", comment: ""), image: UIImage(named: "settings")!)
    ]
}

}

CodePudding user response:

tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal)

Try this solution

  • Related