如何在 Swift 中更改 UIBarButtonItem 的 tintColor? [英] How to change tintColor of UIBarButtonItem in Swift?

查看:28
本文介绍了如何在 Swift 中更改 UIBarButtonItem 的 tintColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change the color of my right bar button item from black to white. It is a button as a search icon. I have not coded the search implementation yet as I want to get the main interface completed first. I thought that I'd written the correct codes so it should appear as white, but it seems to still appear as black in both the storyboard and simulator.

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

In the storyboard, I have also set it to white.

这是我的代码,它位于 AppDelegate.swift 文件中:

Here is my code, which is located in the AppDelegate.swift file:

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
}

这是模拟器的图像:

我觉得这行代码不起作用很奇怪.有什么解决办法吗?

I find it odd that this line of code doesn't work. Any solution?

推荐答案

问题是按钮自动设置为自定义.我把它改成了系统.

The problem was that the button was automatically set as custom. I refigured it to system.

这篇关于如何在 Swift 中更改 UIBarButtonItem 的 tintColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆