我需要在导航栏中显示我的应用程序图标。为此,我将其添加为右键按钮项。我不希望它是可点击的,我只需要那里的图标,所以我将它设置为禁用。这个问题是图标显示为灰色,而不是绿色。有没有办法禁用此按钮,但也保持原始颜色?
答案 0 :(得分:6)
试试这个:
let barButtonItem = UIBarButtonItem(title: "Click", style: .Done, target: self, action: #selector(didClick(_:)))
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Normal)
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Disabled)
barButtonItem.enabled = false
navigationItem.setRightBarButtonItem(barButtonItem, animated: false)
答案 1 :(得分:1)
尝试
[button setBackgroundImage:yourIconImage forState:UIControlStateDisabled];
[button setEnabled:NO]
答案 2 :(得分:0)
myBarButtonItem.setBackgroundImage(myIconImage, forState: .Normal, barMetrics: .Default)
myBarButtonItem.setBackgroundImage(myIconImage, forState: .Disabled, barMetrics: .Default)
myBarButtonItem.enabled = false
答案 3 :(得分:0)
接受的答案对我不起作用(我认为这是因为我使用的是图像,而不是文字)。
您可以使用自定义视图初始化UIBarButtonItem,因此为了解决问题,我将UIBarButtonItem子类化,以便将UIButton作为初始化器参数。
这是代码。
class TintableBarButtonItem: UIBarButtonItem {
private(set) var button: UIButton!
override var tintColor: UIColor? {
get { return button.tintColor }
set { button.tintColor = newValue }
}
convenience init(button: UIButton) {
self.init(customView: button)
self.button = button
button.imageView?.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 34, height: 30)
}
}
我在ViewController中使用它:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
let image = #imageLiteral(resourceName: "MyIcon").withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
let barButton = TintableBarButtonItem(button: button)
navigationItem.rightBarButtonItem = barButton
}
我使用此处的方法获得了框架尺寸: https://stackoverflow.com/a/45374012/6167296
您还必须在按钮上设置目标操作。 您可以在此处看到类似的答案:https://stackoverflow.com/a/2796488/6167296
答案 4 :(得分:0)
要在整个应用中设置禁用状态的颜色,您可以在AppDelegate
的{{1}}中执行以下操作:
application:didFinishLaunchingWithOptions:
答案 5 :(得分:0)
快速4.X
let barButtonItem = UIBarButtonItem(title: "Click", style: .done, target: self, action: #selector(didClick(_:)))
barButtonItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.blueColor()], forState: .normal)
barButtonItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.blueColor()], forState: .disabled)
barButtonItem.enabled = false
navigationItem.setRightBarButtonItem(barButtonItem, animated: false)
基本上NSForegroundColorAttributeName
变成了NSAttributedStringKey.foregroundColor
,状态从大写变成了小写.Disabled
变成了.disabled
等。
答案 6 :(得分:0)
SWIFT 5
let barButtonItem = UIBarButtonItem(title: "Click", style: .done, target: self, action: #selector(didClick(_:)))
barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: .normal)
barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: .disabled)
barButtonItem.isEnabled = false
navigationItem.setRightBarButton(barButtonItem, animated: false)