Changing tab bar item image and text color iOS

前端 未结 22 1163
说谎
说谎 2020-12-02 06:10

Here is my tab bar:

\"enter

The following image shows the program being run an

22条回答
  •  忘掉有多难
    2020-12-02 07:07

    This Code works for Swift 4 if you want to change the image of Tab Bar Item when pressed. Copy and paste in the first viewDidLoad method that is hit in the project

       let arrayOfImageNameForSelectedState:[String] = ["Image1Color", "Image2Color","Image3Color"]
       let arrayOfImageNameForUnselectedState: [String] = ["Image1NoColor","Image2NoColor","Image3NoColor"]
    
    
        print(self.tabBarController?.tabBar.items?.count)
    
        if let count = self.tabBarController?.tabBar.items?.count {
            for i in 0...(count-1) {
                let imageNameForSelectedState   = arrayOfImageNameForSelectedState[i]
                print(imageNameForSelectedState)
                let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
                print(imageNameForUnselectedState)
                self.tabBarController?.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
                self.tabBarController?.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
            }
        }
    

提交回复
热议问题