给导航标题的颜色

时间:2018-04-12 09:39:58

标签: ios

我需要在导航标题中显示textcolour white。 我尝试了一些代码。但颜色没有改变。现在我的代码如下。

在Appdelegate中

: -

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    //to set the background status bar......
    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor =  UIColor(red: 0.215, green: 0.086, blue: 0.298, alpha:1.0)

    }//to set the light color
    UIApplication.shared.statusBarStyle = .lightContent
         //declare the tab controller

    let tabBarController = QM_TabBarController()

    let navigationController = UINavigationController(rootViewController: tabBarController)
    navigationController.isNavigationBarHidden = false;

    //setting the navigation color
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.27, green: 0.14, blue: 0.36, alpha:1.0)
        UINavigationBar.appearance().tintColor = UIColor.white // Do only if you want


    //make the tab bar controller as root
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = tabBarController
    window?.makeKeyAndVisible()

          return true
    }

func uicolorFromHex(rgbValue:UInt32)->UIColor{
    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
    let blue = CGFloat(rgbValue & 0xFF)/256.0
    return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}

在TabBarController中,如下所示。

    let controllers = [viewController1, viewController2,viewController3,viewController4,viewController5]
         self.viewControllers = controllers.map { UINavigationController(rootViewController: $0)}
           self.viewControllers = controllers


    UITabBar.appearance().barTintColor = UIColor(red: 0.27, green: 0.125, blue: 0.36, alpha:1.0)

     viewController1.tabBarItem = UITabBarItem(title: "HOME", image: #imageLiteral(resourceName: "HOME"), selectedImage: #imageLiteral(resourceName: "HOME1"))

 viewController1.navigationItem.title = "HOME"

这里显示黑色,但我需要白色标题。

2 个答案:

答案 0 :(得分:2)

请在appDelegate中写下以下代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name:"Montserrat-SemiBold", size: 15.0)!]
    UINavigationBar.appearance().tintColor = UIColor.white // Do only if you want

    return true
}

答案 1 :(得分:1)

如果您想要提供导航标题颜色,请尝试使用

self.navigationController?.navigationBar.tintColor = .white
self.navigationItem.title = "FIRST"
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]