Search code examples
iosswiftuitabbarcontrolleruitabbaritemtintcolor

Change Tabbar background color in override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!)


I need to load tabbar items. Here i need different background colors for tabbar in different tabs . i am changing bar tint color in didSelectItem. But it's background color is not changing . while loading tab bar it is working fine .

Here is my code

 override func viewDidLoad() {

         if(tabIndex == 1){
            UITabBar.appearance().tintColor = UIColor.whiteColor()
            UITabBar.appearance().barTintColor = Colors.TAB_BAR_ALBUM_BG_COLOR
            self.view.backgroundColor = Colors.TAB_BAR_ALBUM_BG_COLOR

        }else if(tabIndex == 2){
            UITabBar.appearance().tintColor = UIColor.whiteColor()
            UITabBar.appearance().barTintColor = Colors.TAB_BAR_ME_BG_COLOR
            self.view.backgroundColor = Colors.TAB_BAR_ME_BG_COLOR

        }
    }

While loading tabbar tint color is loading fine

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {

        UITabBar.appearance().tintColor = UIColor.whiteColor()

        if(item.tag == 1){
            UITabBar.appearance().barTintColor = Colors.TAB_BAR_ALBUM_BG_COLOR
            self.view.backgroundColor = Colors.TAB_BAR_ALBUM_BG_COLOR

        }else if(item.tag == 2){
            UITabBar.appearance().barTintColor = Colors.TAB_BAR_ME_BG_COLOR
            self.view.backgroundColor = Colors.TAB_BAR_ME_BG_COLOR

        }
    }

While changing tabbar items it's not working .


Solution

  • Got Solution , making Completely transparent UITabBar in app delegate

    [[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
    [[UITabBar appearance] setBackgroundImage:[UIImage new]];
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    

    And changing background color on didSelectItem

    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
        println(" selected index \(item.tag)")
    
        if(item.tag == 0){
            dismissViewControllerAnimated(true, completion: nil)
        }
    
        if(item.tag == 1){
            self.view.backgroundColor = Colors.TAB_BAR_ALBUM_BG_COLOR
    
        }else if(item.tag == 2){
            self.view.backgroundColor = Colors.TAB_BAR_ME_BG_COLOR
    
        }
    }