如何在Swift中设置右UIBarButtonItem的色调?

时间:2017-01-23 01:32:30

标签: ios swift uinavigationcontroller uibarbuttonitem uibarbuttonitemstyle

我在viewDidLoad中使用以下代码向我的导航控制器添加了一个右侧栏按钮:

var b = UIBarButtonItem(
        image: UIImage(named: "settings"),
        style: .plain,
        target: self,
        action: #selector(sayHello(sender:))
    )

    self.navigationItem.rightBarButtonItem = b

我的settings png文件是白色的,但是当我运行该应用时,我会看到默认的blue颜色。无论如何,我想把它变成红色。我该怎么办?

我试过了:

let navigationBarAppearnce = UINavigationBar.appearance()
navigationBarAppearnce.tintColor = UIColor.red

但它不起作用。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

你试过了吗?

let navigationController = UINavigationController() //not how you should actually get it, but just for purpose of example
navigationController.navigationBar.tintColor = UIColor.red

修改

我实际使用以下内容:

navigationController.navigationBar.barTintColor = UIColor.red