更改 iOS 导航栏的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39931463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of iOS Navigation Bar
提问by Anthony Shahine
I'm trying to change the color of my navigator bar but I found that it's only impossible if the navigator is the root one.
我正在尝试更改导航栏的颜色,但我发现如果导航栏是根导航栏,这是不可能的。
I'm trying this:
我正在尝试这个:
self.navigationController?.navigationBar.translucent = true
self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()
All my Viewcontrollers
are related to navigator controllers. However nothing is changed. In fact I tried to make the same things from storyboard but it works only if I'm in the first navigator.
我所有的Viewcontrollers
都与导航控制器有关。然而什么都没有改变。事实上,我试图从情节提要中制作相同的东西,但只有当我在第一个导航器中时它才有效。
I tried to read everything related to this problem but found nothing
我试图阅读与此问题相关的所有内容,但一无所获
I could add any item to the navigator bar like this
我可以像这样将任何项目添加到导航栏
let HomeImage = UIImage(named: "home")!
let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage, style: .Plain, target: self, action: "home:")
navigationItem.rightBarButtonItem = Home
回答by Anthony Shahine
In fact, i found that the solution was to use in the AppDelegate.siwft
:
事实上,我发现解决方案是在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
}
and then in each view controller, that we need another background color or something else
然后在每个视图控制器中,我们需要另一种背景颜色或其他颜色
the segue should be different than "show"
use the func
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()] }
segue 应该与“show”不同
使用函数
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()] }
回答by iAj
Updated for Swift 3
为 Swift 3 更新
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4
斯威夫特 4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 5
斯威夫特 5
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
回答by Hemang
Swift 4.2:
斯威夫特 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:
斯威夫特 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]
回答by Yogesh Mv
For changing the navigation bar theme color throughout the full app you can do this by using UiNavigation bar appearance.
要在整个应用程序中更改导航栏主题颜色,您可以使用 UiNavigation 栏外观来完成此操作。
UINavigationBar.appearance().barTintColor = UIColor.redColor()
回答by KhanShaheb
If your view controller is embedded in navigation controller then you can remove this default navigation bar and can use a custom navigation bar for that view controller.
如果您的视图控制器嵌入在导航控制器中,那么您可以删除此默认导航栏,并可以为该视图控制器使用自定义导航栏。
Then you can do look like
然后你可以看起来像
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
回答by nocholla
Make the following update to the AppDelegate.Swift file i.e.
UINavigationBar.appearance().barTintColor = UIColor(red:x.xx, green:x.xx, blue:x.xx, alpha:1.0)
对 AppDelegate.Swift 文件进行以下更新,即
UINavigationBar.appearance().barTintColor = UIColor(red:x.xx, green:x.xx, blue:x.xx, alpha:1.0)
Refer to example below
参考下面的例子
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:.
}
}
回答by Hitesh Chauhan
self.navigationController?.navigationBar.barTintColor = UIColor.gray
回答by Zaid Pathan
For black nav bar try this:
对于黑色导航栏,试试这个:
navigationController?.navigationBar.barStyle = .black
回答by amisha.beladiya
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];