更改iOS导航栏的颜色

更改iOS导航栏的颜色,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,我试图更改导航栏的颜色,但我发现只有当导航栏是根导航栏时才不可能 我正在尝试这个: self.navigationController?.navigationBar.translucent = true self.navigationController!.navigationBar.barTintColor = UIColor.blueColor() 所有myViewController都与navigator控制器相关。然而,一切都没有改变。事实上,我试着从故事板上制作同样的东西,但只有当

我试图更改导航栏的颜色,但我发现只有当导航栏是根导航栏时才不可能

我正在尝试这个:

self.navigationController?.navigationBar.translucent = true

self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()
所有my
ViewController
都与navigator控制器相关。然而,一切都没有改变。事实上,我试着从故事板上制作同样的东西,但只有当我在第一个导航器中时,它才起作用

我试图阅读与这个问题有关的所有内容,但什么也没找到

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

let HomeImage = UIImage(named: "home")!
    let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
    navigationItem.rightBarButtonItem = Home

要在整个应用程序中更改导航栏主题颜色,您可以使用UiNavigation bar外观进行更改

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

如果视图控制器嵌入在导航控制器中,则可以删除此默认导航栏,并可以为该视图控制器使用自定义导航栏

然后你就可以看起来像

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

事实上,我发现解决方案是在
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
}
然后在每个视图控制器中,我们需要另一种背景色或其他颜色

  • segue应该不同于“show”

  • 使用func
    视图将出现

     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()]
    }
    

  • 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]
    
    //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]
    
    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 = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
    //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]
    

    为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
    

    对AppDelegate.Swift文件进行以下更新,即。
    UINavigationBar.appearance().bartincolor=UIColor(红色:x.xx,绿色:x.xx,蓝色: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:.
    }
    
    }
    

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

    navigationController?.navigationBar.barStyle = .black
    

    为SwiftUI解决此问题:

                init() {
            //         For .navigationBarTitle(Text("Update"), displayMode: .automatic)
            
            let coloredAppearance = UINavigationBarAppearance()
            coloredAppearance.backgroundColor = .black
            coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            
            // to make everything work normally
            UINavigationBar.appearance().standardAppearance = coloredAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
            
            //         For .navigationBarTitle(Text("Update"), displayMode: .inline)
            
            //        UINavigationBar.appearance().barTintColor = .black
            //        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]
        }
        
        var body: some View {
            NavigationView {
                //                     Text("Screen")
                let HomeImage = UIImage(named: "home")!
                let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
                navigationItem.rightBarButtonItem = Home
                    .navigationBarTitle(Text("Update"), displayMode: .automatic)
            }
            // that means only show one view at a time no matter what device I'm working
            .navigationViewStyle(StackNavigationViewStyle())
        }
    

    我必须在appdelegate.siwft文件中执行此操作吗?func应用程序(uApplication:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:Any]?)->Bool{//setup navBar….}@PrashannaChudal是的,你可以。。在上面的appdelegate方法中对其进行编码(请参见上面的注释)…它可以工作,但不能在两个控制器之间进行无缝转换使用未解析标识符“NSForegroundColorAttributeName”@Takasur用于swift 4,NSAttributedStringKey.foregroundColorAnd,同时确保没有背景图像(覆盖背景色)在导航栏中。我在那里不小心被别人设置了,我花了几个小时才检查出来…@Anthony我还有一个问题。这将成功更改标题文本的颜色。但是如果按下另一个视图控制器,然后返回,标题文本颜色仍然没有改变。Swift 4.2,使用
    NSAttributedString.Key.foregroundColor
    。出于某种奇怪的原因,Xcode 10中的自动完成并没有建议BartinColor作为UINavigationBar.appearance()的属性,但是复制/粘贴代码确实可以编译和工作。谢谢有关最新答案,请参阅此链接,此链接仅更改样式,而不是条形图颜色。:)