我正在尝试将“设置”按钮的颜色更改为白色,但无法将其更改。
我试过这两个:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
如何将该按钮设为白色?
在AppDelegate
中的didFinishLaunchingWithOptions
类中使用此代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这个片段带来了魔力。而不是redColor,将其更改为您的愿望。
让我们试试这段代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.whiteColor() // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.purpleColor() // Bar's background color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] // Title's text color
self.window?.backgroundColor = UIColor.whiteColor()
return true
}
在swift 2.0中使用
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
如果您已经在“设置”视图控制器中有后退按钮,并且您想要将“付款信息”视图控制器上的后退按钮颜色更改为其他内容,则可以在“设置”视图中执行此操作控制器准备这样的segue :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourPaymentInformationSegue"
{
//Make the back button for "Payment Information" gray:
self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray
}
}
设置UINavigationBar.appearance().tintColor
的所有答案都与UIAppearance.h
中的Apple文档冲突。
iOS7的注意事项:在iOS7上,
tintColor
属性已移至UIView
,现在具有UIView.h
中描述的特殊继承行为。此继承行为可能与外观代理冲突,因此现在不允许tintColor
使用外观代理。
在Xcode中,您需要按命令单击要与外观代理一起使用的每个属性,以检查头文件并确保使用UI_APPEARANCE_SELECTOR
注释该属性。
因此,通过外观代理为整个应用程序着色导航栏紫色以及标题和按钮白色的正确方法是:
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white
请注意,UIBarButtonItem
不是UIView
的子类,而是NSObject
。所以它的tintColor
财产不是来自tintColor
的继承的UIView
。
不幸的是,UIBarButtonItem.tintColor
没有使用UI_APPEARANCE_SELECTOR
注释 - 但在我看来这是一个文档错误。 Apple Engineering在this radar的回应表明它得到了支持。
将以下代码添加到AppDelegate.swift中的didFinishLaunchingWithOptions函数
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade
// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
对于Swift 2.0,要在AppDelegate.swift中使用以下内容更改导航栏色调颜色,标题文本和后退按钮色调颜色
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Navigation bar tint color change
UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)
//Back button tint color change
UINavigationBar.appearance().barStyle = UIBarStyle.Default
UINavigationBar.appearance().tintColor = UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)
//Navigation Menu font tint color change
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
return true
}
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
这适用于我,iOS 9.0+
此代码更改箭头颜色
self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
如果这不起作用,请使用以下代码:
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
Swift 3笔记
UIColor.whiteColor()
和类似的已被简化为UIColor.white
此外,许多以前隐式的选项已更改为显式,因此您可能需要:
self.navigationController?.navigationBar =
你有一个选择隐藏你的后退按钮,并与你自己。然后设置它的颜色。
我这样做了:
self.navigationItem.setHidesBackButton(true, animated: true)
let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
self.navigationItem.leftBarButtonItem = backbtn
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()
不知道为什么没有人提到这个...但我正在做你在我的viewDidLoad
做的正确...而且它不起作用。然后我将我的代码放入viewWillAppear
,这一切都奏效了。
上面的解决方案是更改单个barbuttonItem。如果要更改代码中每个navigationBar的颜色,请按照this answer进行操作。
使用appearance()
基本上改变类本身就像在应用程序中对该视图的所有实例进行全局更改。有关更多信息,请参阅here
它将通过 - (void)viewDidLoad中的这一行解决:
self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
你应该添加这一行
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
我更喜欢自定义NavigationController而不是设置全局ui,或者放入ViewController。
这是我的解决方案
class AppNavigationController : UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
}
}
extension AppNavigationController : UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let backButtonItem = UIBarButtonItem(
title: " ",
style: UIBarButtonItem.Style.plain,
target: nil,
action: nil)
backButtonItem.tintColor = UIColor.gray
viewController.navigationItem.backBarButtonItem = backButtonItem
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
}
}
如果你使用像UIBarButtonItem.appearance().tintColor = .white
这样的全局设置,你也不需要像使用EKEventEditViewController,PickerViewController等Apple Api一样混乱
你应该用这个:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
你可以像这样使用。把它放在AppDelegate.swift
里面。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
return true
}
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
在Swift3中,将“后退”按钮设置为red
。
self.navigationController?.navigationBar.tintColor = UIColor.red
在Swift 4中,您可以使用以下方法处理此问题:
let navStyles = UINavigationBar.appearance()
// This will set the color of the text for the back buttons.
navStyles.tintColor = .white
// This will set the background color for navBar
navStyles.barTintColor = .black
更改完整的应用主题
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().tintColor = .white
return true
}
更改特定控制器
let navController = UINavigationController.init(rootViewController: yourViewController)
navController.navigationBar.tintColor = .red
present(navController, animated: true, completion: nil)