Ios 在导航栏中插入图标/使BarButtonItem不可单击

Ios 在导航栏中插入图标/使BarButtonItem不可单击,ios,swift,icons,navigationbar,Ios,Swift,Icons,Navigationbar,我想在导航栏中放置一个蓝牙图标,以显示已连接/已断开连接的状态 我尝试添加一个BarButtonItem,将图像设置为我的蓝牙图标,并禁用和启用了此按钮。到目前为止,它工作得很好,在我看来还不错,但我不想让这个按钮可以点击,这样它就不会在点击图标时改变颜色 这是可能的,还是有办法将UIImageView放入导航栏 谢谢 尝试使用以下方法禁用触摸事件: myBarButtonItem.isUserInteractionEnabled=false navigationItem.RightBarBut

我想在导航栏中放置一个蓝牙图标,以显示已连接/已断开连接的状态

我尝试添加一个BarButtonItem,将图像设置为我的蓝牙图标,并禁用和启用了此按钮。到目前为止,它工作得很好,在我看来还不错,但我不想让这个按钮可以点击,这样它就不会在点击图标时改变颜色

这是可能的,还是有办法将UIImageView放入导航栏


谢谢

尝试使用以下方法禁用触摸事件:

myBarButtonItem.isUserInteractionEnabled=false

navigationItem.RightBarButtonim?.isEnabled=false

添加以下代码将解决您的问题

let btnBluetooth = UIButton()
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .normal)
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .highlighted)
btnBluetooth.tintColor = .red

let barButton = UIBarButtonItem(customView: btnBluetooth)
self.navigationItem.rightBarButtonItem = barButton
此代码将添加自定义按钮,您可以在其中管理正常和高亮模式的图像。对于蓝牙状态管理,将您在
UIBarButtonItem
中添加的
ui按钮的
tintColor
更改为自定义视图

若你们点击按钮,这不会改变图像的颜色

如果不想添加
ui按钮
,可以通过以下代码添加
ui图像视图

let imgBluetooth = UIImageView(image: #imageLiteral(resourceName: "icon_bluetooth"))
imgBluetooth.tintColor = .red

let barButton = UIBarButtonItem(customView: imgBluetooth)
self.navigationItem.rightBarButtonItem = barButton
另外,请确保已为添加在
Assets.xcsets
中的蓝牙图标选择了渲染为模板图像,以影响颜色。否则,图像将显示为原始图像。见下文:


不幸的是,UIBarButtonims似乎没有isUserInteractionEnabled属性:(我使用isEnabled=false来显示未连接的状态。这很好,但我也想显示已连接的状态,但我无法在禁用tintColor后立即更改它,这就是为什么我需要一种解决方法。将其转换为自定义UIButton?rightNavButton=UIButton(类型:。自定义),然后以这种方式进行自定义?