How to properly customize UITabBar and UITabBarItem on iOS 7 and iOS 8?

后端 未结 2 1565
有刺的猬
有刺的猬 2020-12-31 07:01

I am googling around so much, but nowhere I find a straight and consolidated answer.

I want to customize myUITabBarController such tha

相关标签:
2条回答
  • 2020-12-31 07:47

    I had the same problem as you. As far as I know there is no difference for the different iOS versions.
    I solved it programmatically like this:

    1. Turning the bar color black works as following (You already said it) (in AppDelegate):

      UITabBar.appearance().barTintColor = UIColor.blackColor()

    2. To set the color of the title for the different states, I used this code (in AppDelegate):

      UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName!: UIColor.redColor()], forState:.Selected)
      UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName!: UIColor.whiteColor()], forState:.Normal)
      
    3. (and 4.) You can achieve the different item colors, multicolored icons and different item colors for the different states, by setting the image programmatically and change the rendering mode (imageWithRenderingMode:) to UIImageRenderingMode.AlwaysOriginal, this looks as following (do this in the first view controller class for all your view controllers):

      var recentsItem = self.tabBarController!.tabBar.items![0] as UITabBarItem
      var recentsItemImage = UIImage(named:"recents.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
      var recentsItemImageSelected = UIImage(named: "recentsSelected.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
      recentsItem.image = recentsItemImage
      recentsItem.selectedImage = recentsItemImageSelected
      

    I hope this helps, if you have any following questions, feel free to ask me.

    0 讨论(0)
  • 2020-12-31 08:02

    Solution provided by smudis is great and because I don't have enough reputation to comment I decided to post the 3rd part of smudis' solution in Objective-C, in case it might help somebody:

    To get a reference of the tabBar type the above code into AppDelegate's method application:didFinishLaunchingWithOptions:

    UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
    UITabBar *tb = tbc.tabBar;
    

    Then the image adjustment can be done as follows:

    for (UITabBarItem *tbi in tb.items) {
        tbi.selectedImage = [tbi.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tbi.image = [tbi.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    0 讨论(0)
提交回复
热议问题