【问题标题】:Change the color of iOS Navigation Bar更改 iOS 导航栏的颜色
【发布时间】:2017-02-17 07:01:09
【问题描述】:

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

我正在尝试这个:

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


【解决方案1】:

其实我发现解决办法是在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()]
    }
    

【讨论】:

  • 它可以工作,但它不能在两个控制器之间进行无缝转换
  • 使用未解析的标识符“NSForegroundColorAttributeName”
  • @Takasur 用于 swift 4,NSAttributedStringKey.foregroundColor
  • 还要确保您的导航栏中没有背景图片(覆盖背景颜色)。我不小心把我的放在那里了,我花了几个小时才检查出来......
  • @Anthony 我还有一个问题。这样就成功改变了标题文字颜色。但是如果 push 另一个视图控制器然后返回,标题文本颜色仍然没有改变。
【解决方案2】:

为 Swift 3 更新

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

斯威夫特 4

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

斯威夫特 5

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

【讨论】:

  • 我必须在 appdelegate.siwft 文件中执行此操作吗?
  • func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 设置导航栏..... }
  • @PrashannaChudal 是的,你可以.. 在上面的 appdelegate 方法中编码(见上面的评论)..
【解决方案3】:

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]

【讨论】:

  • Swift 4.2,请改用NSAttributedString.Key.foregroundColor
  • 出于某种奇怪的原因,Xcode 10 中的自动完成功能并未提议 barTintColor 作为 UINavigationBar.appearance() 的属性,但复制/粘贴您的代码确实可以编译和工作。谢谢!
【解决方案4】:

要更改整个应用程序中的导航栏主题颜色,您可以使用 UiNavigation 栏外观来完成此操作。

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

【讨论】:

    【解决方案5】:

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

    然后你可以做样子

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

    【讨论】:

      【解决方案6】:

      对 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:.
      }
      
      }
      

      【讨论】:

        【解决方案7】:
        self.navigationController?.navigationBar.barTintColor = UIColor.gray
        

        【讨论】:

          【解决方案8】:

          对于黑色导航栏试试这个:

          navigationController?.navigationBar.barStyle = .black
          

          【讨论】:

          • 这只会改变样式,不会改变条形颜色。 :)
          【解决方案9】:
          self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
          

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-02-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多