更改iOS导航栏的颜色

问题描述 投票:48回答:9

我正在尝试更改导航器栏的颜色,但是我发现只有导航器是根导航器,这是不可能的。

我正在尝试:

self.navigationController?.navigationBar.translucent = true

self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()

我的全部Viewcontrollers与导航器控制器有关。但是什么都没有改变。实际上,我试图从情节提要中进行相同的操作,但仅当我在第一个导航器中时,它才有效。

我试图阅读与此问题有关的所有内容,但未发现任何内容

我可以将任何项目添加到导航栏,就像这样

let HomeImage = UIImage(named: "home")!
    let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
    navigationItem.rightBarButtonItem = Home
ios swift uinavigationcontroller
9个回答
96
投票

实际上,我发现解决方案将在AppDelegate.siwft中使用:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

    return true
}

然后在每个视图控制器中,我们需要其他背景色或其他颜色

  1. 该segue应该不同于“ show”

  2. 使用功能viewWillAppear

     override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)
         self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
         self.navigationController?.navigationBar.tintColor = UIColor.blueColor()
         self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]
    }
    

49
投票

已为Swift 3更新

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

Swift 4

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

Swift 5

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

20
投票

Swift 4.2

    //To change Navigation Bar Background Color
    UINavigationBar.appearance().barTintColor = UIColor.blue
    //To change Back button title & icon color
    UINavigationBar.appearance().tintColor = UIColor.white
    //To change Navigation Bar Title Color
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

Swift 3.x

//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

7
投票

要在整个应用程序中更改导航栏主题颜色,可以通过使用UiNavigation栏外观来完成。

UINavigationBar.appearance().barTintColor = UIColor.redColor()

4
投票

如果您的视图控制器嵌入在导航控制器中,那么您可以删除此默认导航栏,并且可以将自定义导航栏用于该视图控制器。

然后您可以看起来像

UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)

4
投票

[对AppDelegate.Swift文件进行以下更新,即UINavigationBar.appearance().barTintColor = UIColor(red:x.xx, green:x.xx, blue:x.xx, alpha:1.0)

请参阅下面的示例

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.
    UINavigationBar.appearance().barTintColor = UIColor(red:0.03, green:0.25, blue:0.11, alpha:1.0)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

2
投票
self.navigationController?.navigationBar.barTintColor = UIColor.gray

1
投票

对于黑色导航栏,请尝试以下操作:

navigationController?.navigationBar.barStyle = .black

0
投票
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
© www.soinside.com 2019 - 2024. All rights reserved.