我正在尝试将“设置”按钮的颜色更改为白色,但无法更改。
这两个我都试过了:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
https://i.stack.imgur.com/vqCQh.png
如何使那个按钮变白?
您可以通过单击板上的空白区域并在右侧工具栏中选择“显示文件检查器”来更改情节提要中的全局色调颜色,您将在工具栏底部看到“全局色调”选项。
https://i.stack.imgur.com/BTbJU.png
此代码更改箭头颜色
self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
如果这不起作用,请使用以下代码:
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
斯威夫特 3 笔记
UIColor.whiteColor()
和类似内容已简化为 UIColor.white
此外,许多以前的隐式选项已更改为显式,因此您可能需要:
self.navigationController?.navigationBar =
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
为我工作(Swift 2.2)
在情节提要中很容易设置:
https://i.stack.imgur.com/0hrE5.png
https://i.stack.imgur.com/IHeMk.png
你应该使用这个:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
斯威夫特 5.5
更改完整的应用主题
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set for app
UINavigationBar.appearance().tintColor = .white
return true
}
更改特定控制器
let navController = UINavigationController.init(rootViewController: yourViewController)
navController.navigationBar.tintColor = .red
present(navController, animated: true, completion: nil)
UINavigationController
更改它怎么办?
你可以像这样使用。将其放在 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
}
在 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
self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation
self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
斯威夫特 3
对于 Swift 3,最受好评的答案是不正确的。
https://i.stack.imgur.com/GJN88.png
改变颜色的正确代码是:
self.navigationController?.navigationBar.tintColor = UIColor.white
如果要更改颜色,请将上面的 UIColor.white 更改为所需的颜色
所有答案设置 UINavigationBar.appearance().tintColor
与 Apple 在 UIAppearance.h
中的文档相冲突。
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
属性不是从 UIView
继承的 tintColor
。
不幸的是,UIBarButtonItem.tintColor
没有用 UI_APPEARANCE_SELECTOR
注释 - 但在我看来,这似乎是一个文档错误。 Apple Engineering 在 this radar 中的响应表明它是受支持的。
在 didFinishLaunchingWithOptions
内的 AppDelegate
类中使用此代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这个片段有魔力。而不是 redColor,根据您的意愿将其更改为。
如果您尝试了很多次但无法正常工作,您可以尝试:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
实际上,我尝试了很多次,才发现这种方法行得通。
让我们试试这段代码:
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
}
}
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
这对我有用,iOS 9.0+
不知道为什么没有人提到这一点......但我正在做你在我的 viewDidLoad
中所做的事情......但它没有工作。然后我将代码放入 viewWillAppear
中,一切正常。
上面的解决方案是改变一个single barbuttonItem。如果您想更改代码中 每个 导航栏的颜色,请按照 this answer 操作。
基本上,使用 appearance()
更改类本身就像在您的应用程序中对该视图的所有实例进行全局更改。如需更多信息,请参阅here
斯威夫特 5 更新
如果您需要全局设置 Back
按钮颜色,您可以简单地使用:
UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color
然后您不需要在每个视图控制器上设置后退按钮背景颜色。如果你使用这个,你不能在另一个视图控制器上设置返回按钮颜色
但
如果您需要在视图控制器上设置后退按钮颜色或在另一个视图控制器上更改,请不要使用上述方法。你可以使用:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
.foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
navigationController?.navigationBar.tintColor = .green // Back button color
在 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.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()
我更喜欢自定义 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
这样的全局设置 ui,您也不需要像 EKEventEditViewController、PickerViewController 等那样弄乱 Apple Api
我在 swift 5 中使用它并为我工作
navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
它将通过 -(void)viewDidLoad 中的这一行来解决:
self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
(lldb) p self.navigationItem.backBarButtonItem (UIBarButtonItem *) $9 = nil (lldb)
你应该添加这一行
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
斯威夫特 5.3:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
出于某种原因,它不适用于我的编程方式。但是,每次都将其设置在情节提要中。
https://i.stack.imgur.com/x9854.png
不定期副业成功案例分享
AppDelegate.swift > application(application:didFinishLaunchingWithOptions:)
中编写:self.window!.tintColor = .yourColor