繁体   English   中英

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

[英]Set Nav Bar's color without using .barTintColor?

我试图显示用户图片代替rightBarButton但是设置UINavigationBar barTintColor使图像着色。

在此处输入图片说明

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

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

提前致谢

更改tintColor

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

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

 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
}

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM