如何更改 Swift 中 UIBarButtonItem 的 tintColor?
How to change tintColor of UIBarButtonItem in Swift?
我想将右侧栏按钮项的颜色从黑色更改为白色。它是一个按钮作为搜索图标。我还没有编写搜索实现的代码,因为我想先完成主界面。我以为我写了正确的代码所以它应该显示为白色,但它似乎在故事板和模拟器中仍然显示为黑色。
在storyboard里,我也设置成白色了。
这是我的代码,位于 AppDelegate.swift
文件中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Changing the status bar's colour to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
// Changing the navigation controller's background colour
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
// Changing the navigation controller's title colour
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
// Changing the colour of the bar button items
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Changing the tint colour of the tab bar icons
UITabBar.appearance().tintColor = UIColor(red: 0.0/255/0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
return true
}
这是模拟器的图片:
我觉得这行代码不起作用很奇怪。有什么解决办法吗?
问题是按钮被自动设置为自定义。我改成了系统。
SWIFT 4
将自定义 barbuttonitem 声明为出口后:
@IBOutlet weak var yourBarButtonItem: UIBarButtonItem!
您可以随心所欲地定义颜色。
yourBarButtonItem.tintColor = .red
设置普通文本的文本颜色:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.tintColor = UIColor.appColor
navigationItem.rightBarButtonItems = [uiBarButton]
设置属性文本的文本颜色:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.appColor], for: .normal)
navigationItem.rightBarButtonItems = [uiBarButton]
我想将右侧栏按钮项的颜色从黑色更改为白色。它是一个按钮作为搜索图标。我还没有编写搜索实现的代码,因为我想先完成主界面。我以为我写了正确的代码所以它应该显示为白色,但它似乎在故事板和模拟器中仍然显示为黑色。
在storyboard里,我也设置成白色了。
这是我的代码,位于 AppDelegate.swift
文件中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Changing the status bar's colour to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
// Changing the navigation controller's background colour
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
// Changing the navigation controller's title colour
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
// Changing the colour of the bar button items
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Changing the tint colour of the tab bar icons
UITabBar.appearance().tintColor = UIColor(red: 0.0/255/0, green: 165.0/255.0, blue: 227.0/255.0, alpha: 1.0)
return true
}
这是模拟器的图片:
我觉得这行代码不起作用很奇怪。有什么解决办法吗?
问题是按钮被自动设置为自定义。我改成了系统。
SWIFT 4
将自定义 barbuttonitem 声明为出口后:
@IBOutlet weak var yourBarButtonItem: UIBarButtonItem!
您可以随心所欲地定义颜色。
yourBarButtonItem.tintColor = .red
设置普通文本的文本颜色:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.tintColor = UIColor.appColor
navigationItem.rightBarButtonItems = [uiBarButton]
设置属性文本的文本颜色:
let uiBarButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(logOutTapped))
uiBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.appColor], for: .normal)
navigationItem.rightBarButtonItems = [uiBarButton]