我正在尝试将“设置”按钮的颜色更改为白色,但无法更改。
这两个我都试过了:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
如何使那个按钮变白?
我正在尝试将“设置”按钮的颜色更改为白色,但无法更改。
这两个我都试过了:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
如何使那个按钮变白?
此代码更改箭头颜色
self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
如果这不起作用,请使用以下代码:
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
斯威夫特 3 笔记
UIColor.whiteColor()
和类似的已经简化为UIColor.white
此外,许多以前的隐式选项已更改为显式,因此您可能需要:
self.navigationController?.navigationBar =
你应该使用这个:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
}
更改完整的应用主题
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)
你可以像这样使用。把它放在里面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中,将 Back 按钮设置为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
所有答案设置都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
属性不是继承tintColor
自UIView
。
不幸的是,UIBarButtonItem.tintColor
没有注释UI_APPEARANCE_SELECTOR
- 但在我看来,这似乎是一个文档错误。Apple Engineering 在此雷达中的响应表明它是受支持的。
AppDelegate
在类中使用此代码,在didFinishLaunchingWithOptions
.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这个片段有魔力。而不是 redColor,根据您的意愿将其更改为。
如果您的“设置”视图控制器中已经有后退按钮,并且您想将“付款信息”视图控制器上的后退按钮颜色更改为其他颜色,您可以在“设置”视图控制器中为这样的 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
}
}
在 swift 2.0 中使用
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
让我们试试这段代码:
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
}
如果您尝试了很多次但无法正常工作,您可以尝试:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
实际上,我尝试了很多次,才发现这种方法行得通。
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
这对我有用,iOS 9.0+
在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,要更改Navigation-bar tint color,title text和back button tint color 使用 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) {
}
}
此外,如果您使用全局设置 ui ,您也不需要像EKEventEditViewController、PickerViewController等那样弄乱 Apple ApiUIBarButtonItem.appearance().tintColor = .white
我在 swift 5 中使用它并为我工作
navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
你应该添加这一行
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
它将通过 -(void)viewDidLoad 中的这一行来解决:
self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
斯威夫特 5.3:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")