ios – 如何在Swift中更改UIBarButtonItem的tintColor?

我想将右栏按钮项的颜色从黑色更改为白色.它是一个按钮作为搜索图标.我还没有对搜索实现进行编码,因为我想首先完成主界面.我以为我写了正确的代码所以它应该显示为白色,但它在故事板和模拟器中似乎仍然显示为黑色.

在故事板中,我还将其设置为白色.

这是我的代码,它位于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,alpha: 1.0)

    return true
}

这是模拟器的图像:

我发现这行代码不起作用很奇怪.有解决方案吗

解决方法

SWIFT 4

自定义barbuttonitem声明为插座后:

@IBOutlet weak var yourBarButtonItem: UIBarButtonItem!

您可以根据需要定义颜色.

yourBarButtonItem.tintColor = .red

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...