Search code examples
swift3uinavigationcontrolleruinavigationbarios10uitabbar

Addition of UINavigationBar has changed the background color of UITabBar


So I'm building a tabbed application and the design calls for a UINavigationBar on some of the app's pages. After adding the navigation bar to one of my tabbed ViewControllers, the color of the UITabBar changed from the color that it has been set to originally (bluish-green) to grey. I've tried setting the TabBar Background color in my AppDelegate (which works fine on all other tabs), I've tried setting it locally in the custom TabBar View Controller (does not affect the color at all), I even tried setting it in the specific View Controller itself. I've tried setting a runtim property as well to no avail. I'm at a loss of what to try next. I've posted the code and screen shots below.

AppDelegate.swift

import Firebase
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let myGreenBG = UIColor(colorLiteralRed: 43/255.0, green: 111/255.0, blue: 109/255.0, alpha: 1.0)

    FirebaseApp.configure()

    UITabBar.appearance().backgroundColor = myGreenBG

    UINavigationBar.appearance().backgroundColor = myGreenBG


    return true

}
...

TabBarViewController.swift import UIKit

class tabBarViewController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    let myGreenBG = UIColor(colorLiteralRed: 43/255.0, green: 111/255.0, blue: 109/255.0, alpha: 1.0)

    //self.tabBar.delegate = self

    //Code to render the unselected images in the tab bar
    tabBar.backgroundColor = myGreenBG
    for items in 0 ..< tabBar.items!.count {
        let tabItemIndex = tabBar.items![items]
        tabItemIndex.image = tabItemIndex.image!.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        /*for tabBarItem in (self.tabBarController?.tabBar.items!)!{
            if !(tabBarItem.
            }
        }*/
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
    ...

Correctly displayed correct

Incorrectly displayed Incorrect

Any Advice or tips would be greatly appreciated!


Solution

  • Try below code to change UITabBar background color-

    UITabBar.appearance().barTintColor = UIColor.black
    

    Hope it helps!