Swift 2.2 How to change background color of tab bar controller

后端 未结 3 650
北海茫月
北海茫月 2021-01-02 08:55

I\'ve been trying to change the background of the tab bar, and have read many posts here in ObjC, many of them quite old. But I didn\'t find any for Swift, which is what I\'

相关标签:
3条回答
  • 2021-01-02 09:09

    Xcode 9

    Storyboard

    It possible to set background color directly in view controller (currently in TabBarViewController), for this you need to define Key Path. Don't forget to remove background color on nested view, otherwise it will overlap superview's background color.

    0 讨论(0)
  • 2021-01-02 09:11

    This is a fine way to change the color of a UITabBar. If you want to avoid setting the color in every viewController that is embedded inside of your UITabBarController, you could also create a subclass of UITabBarController and set it there. This way no matter what page comes up first, the color will be set.

    To create a subclass of UITabBarController, just go to file > new > file > cocoa touch class...Then setup your file like in this photo

    Now in your storyboard, set the custom class on your tabBarController

    Finally, in your file you created MyTabBarController (or whatever you called it):

    class MyTabBarController: UITabBarController {
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    
        self.tabBar.barTintColor = .blueColor()
    }
    ...
    
    0 讨论(0)
  • 2021-01-02 09:24

    You can change like this

    tabBarController.tabBar.barTintColor = [UIColor blackColor];

    0 讨论(0)
提交回复
热议问题