在override func tabBar中更改Tabbar背景颜色(tabBar:UITabBar,didSelectItem item:UITabBarItem!)

时间:2015-10-26 07:18:06

标签: ios swift uitabbarcontroller uitabbaritem tintcolor

我需要加载tabbar项目。在这里,我需要不同选项卡中tabbar的不同背景颜色。我正在改变didSelectItem中的条纹色调。但它的背景颜色并没有改变。加载标签栏时它工作正常。

这是我的代码

 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

        }
    }

加载tabbar色彩时颜色正好

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

        }
    }

更改标签栏项目时,它无效。

2 个答案:

答案 0 :(得分:1)

获得解决方案,在app delegate中制作完全透明的UITabBar

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

在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

    }
}

答案 1 :(得分:0)

UITabBar.appearance().barTintColor = Colors.TAB_BAR_ME_BG_COLOR更改为tabBar.barTintColor = UIColor.yellowColor()。希望这有帮助。

相关问题