Setting custom tab icons for selected / not selected

I have icons in my assets that I want to use for my tab bar, 2 for each tab (one of them is not highlighted, but just an outline, and one of them is highlighted, and everything is full). The idea is to use highlighted icons for tabs that are not currently selected and filled in for the selected tab. How can i do this?

+4
source share
2 answers

Just create a class for your tab, for example:

class MainTabBarController: UITabBarController {

     override func viewDidLoad() {
        super.viewDidLoad()

        for item in self.tabBar.items! as [UITabBarItem] {

        // loop through all of your elements in TabBar

        if let image = item.image {

            item.selectedImage = your Selected Image
            item.image = your base Image

        }
    }


}

You can store all your images in an array containing UIImage, for example:

let TabImages = [UIImage]()
let HighlightedImages = [UIImage]()

And then set them in each for each cycle.

+1
var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]
tabItems[2].selectedImage = UIImage(named: "tabImage1_Selected")
+1

Source: https://habr.com/ru/post/1651722/


All Articles