Preserving the original image color of the selected and unselected UITabBar icons

后端 未结 4 741
心在旅途
心在旅途 2021-01-01 13:42

The structure is the following:

In my storyboard, I have a Tab Bar Controller which contains a Tab Bar object. This object has a custom cla

相关标签:
4条回答
  • 2021-01-01 14:21

    Just go to your image assets properties, and set Render as property to "original image"

    And you're good to go!

    0 讨论(0)
  • 2021-01-01 14:29

    Perfect question, really well explained.

    You are not setting imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal for the selected image.

    Just should add imageWithRenderingMode:UIImageRenderingModeAlwaysOriginalto the selectedImage:

      item.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

    Check this other answer.

    0 讨论(0)
  • 2021-01-01 14:34

    Setting non-selected image to always original render and selected one to always template render did the trick for me.

    Solution for iOS 13 and swift 5.1

    let item = UITabBarItem(title: "item_title",
                            image: UIImage(named: "img")?.withRenderingMode(.alwaysOriginal),
                            selectedImage: UIImage(named:"img_selected")?.withRenderingMode(.alwaysTemplate))
    
    0 讨论(0)
  • 2021-01-01 14:46

    Swift 3:

        for item in self.tabBar.items!{
            item.selectedImage = item.selectedImage?.withRenderingMode(.alwaysOriginal)
            item.image = item.image?.withRenderingMode(.alwaysOriginal)
        }
    
    0 讨论(0)
提交回复
热议问题