设置导航栏的颜色而不使用.barTintColor吗?

时间:2019-05-02 04:14:50

标签: swift uinavigationbar viewdidload

我正在尝试显示用户图片来代替rightBarButton,但是将barTintColor中的UINavigationBar设置为彩色图像。

enter image description here

AppDelegate中,尝试过:

UINavigationBar.appearance().barTintColor = .blue

viewDidLoad中,尝试过:

self.navigationController?.navigationBar.backgroundColor = .blue
self.navigationController?.navigationBar.tintColor = .blue
self.navigationController?.navigationBar.barTintColor = .blue

设置图像:

let avatarBtn = UIButton(type: .custom)
avatarBtn.frame = CGRect(x: 0.0, y: 0.0, width: 30, height: 30)
avatarBtn.setImage(UIImage(named:"myAvatar")?.withRenderingMode(.alwaysOriginal), for: .normal)

let menuBarItem = UIBarButtonItem(customView: avatarBtn)
menuBarItem.tintColor = .clear

self.navigationItem.rightBarButtonItem = menuBarItem

这些都不起作用。如何设置不带颜色的彩色导航栏按钮?

预先感谢

2 个答案:

答案 0 :(得分:0)

更改tintColor

tintColor:应用于导航项目和条形按钮项目的色彩。

答案 1 :(得分:0)

我使用了您的代码,但就我而言,我得到了预期的结果。您正在使用哪种图像类型?

 override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.backgroundColor = .blue
    self.navigationController?.navigationBar.tintColor = .blue
    self.navigationController?.navigationBar.barTintColor = .blue
    let avatarBtn = UIButton(type: .custom)
    avatarBtn.frame = CGRect(x: 0.0, y: 0.0, width: 30, height: 30)
    avatarBtn.setImage(UIImage(named:"myAvatar1")?.withRenderingMode(.alwaysOriginal), for: .normal)
    let menuBarItem = UIBarButtonItem(customView: avatarBtn)
    menuBarItem.tintColor = .clear
    self.navigationItem.rightBarButtonItem = menuBarItem
}

enter image description here