Search code examples
iosswiftuitabbartabbar

Customising the UITabBar.appearance in each View Controller [Swift]


I'm trying to set the tab bar to have a different background image on each view controller.

class CharacterVC: UIViewController {

var tabBarApparence = UITabBar.appearance()

override func viewDidLoad() {
    super.viewDidLoad()

    tabBarApparence.backgroundImage = UIImage(named: "BlueTB") //Loaded from Image Asset
}

This works fine and changes it to blue in that view, however when I go to the next view it stays the blue colour and doesn't change to the red colour which I programmed in with this code:

class AnonVC: UIViewController {

var tabBarApparence = UITabBar.appearance()

override func viewDidLoad() {
    super.viewDidLoad()
    tabBarApparence.backgroundImage = UIImage(named: "RedTabBar")
    // addtional code here
}

I have an additional 2 view controllers, one should display the green version of the image and the other the purple version of the image.

Any suggestions which could fix this?


Solution

  • If you want to change appearance of TabBar in a view controller is very easy. You can this in function viewDidLoad or viewWillAppear. The code is the next:

    // Set color of titles and icons in tabBar
    self.tabBarController?.tabBar.tintColor = UIColor.redColor()
    // Set color of background tabBar
    self.tabBarController?.tabBar.barTintColor = UIColor.blueColor()