我正在尝试将“设置”按钮的颜色更改为白色,但是无法更改。
我已经尝试过这两个:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
如何使该按钮变白?
我正在尝试将“设置”按钮的颜色更改为白色,但是无法更改。
我已经尝试过这两个:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它仍然看起来像这样:
如何使该按钮变白?
Answers:
AppDelegate.swift > application(application:didFinishLaunchingWithOptions:)
请输入以下内容:self.window!.tintColor = .yourColor
此代码更改箭头颜色
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)
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.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
}
更改完整的应用主题
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)
UINavigationController
怎么办?
在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
设置的所有答案都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在此雷达中的响应表明它得到了支持。
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();
如果您已经在“设置”视图控制器中具有后退按钮,并且想要将“付款信息”视图控制器上的后退按钮颜色更改为其他颜色,则可以在“设置”视图控制器的这种准备中进行操作:
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
}
}
下面的代码添加到didFinishLaunchingWithOptions函数AppDelegate.swift
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()
您应该添加此行
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) {
}
}
另外,如果您使用ui这样的全局设置,也不需要像EKEventEditViewController,PickerViewController之类的Apple Api那样混乱UIBarButtonItem.appearance().tintColor = .white
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