如何改变的UIBarButtonItem的tintColor斯威夫特?

问题描述 投票:7回答:3

我想我的右边栏按钮项目的颜色改变,从黑到白的。这是一个按钮作为搜索图标。因为我想在主界面首先完成我还没有编码的搜索实现呢。我以为我会写正确的代码,所以应该显示为白色,但它似乎仍然显示为黑色的故事板和模拟两种。

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

这里是我的代码,它位于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
}

这里是模拟器的图像:

enter image description here

我觉得很奇怪,这行代码是行不通的。任何解决方案?

ios swift uinavigationcontroller uinavigationbar uinavigationitem
3个回答
5
投票

SWIFT 4

后声明您的自定义barbuttonitem作为一个出口:

@IBOutlet weak var yourBarButtonItem: UIBarButtonItem!

只要你喜欢,你可以定义你的颜色。

 yourBarButtonItem.tintColor = .red

0
投票

问题是,该按钮被自动设置为自定义。我把它refigured系统。


0
投票

设置文本颜色为普通文本:

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]
© www.soinside.com 2019 - 2024. All rights reserved.