xcode 更改 iOS 中禁用栏按钮项的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35736754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:35:55  来源:igfitidea点击:

Change color of disabled bar button item in iOS

iosxcodeswift

提问by Laura M

I need to display my app's icon in the navigation bar. To do this, I have added it as a right bar button item. I don't want it to be clickable, I just need the icon there, so I set it to disabled. The problem with this is the icon appears grey, instead of green. Is there a way to disable this button but also keep it's original color?

我需要在导航栏中显示我的应用程序图标。为此,我已将其添加为右侧栏按钮项。我不希望它可点击,我只需要那里的图标,所以我将它设置为禁用。问题是图标显示为灰色,而不是绿色。有没有办法禁用此按钮但同时保持其原始颜色?

回答by J.Wang

Try this:

尝试这个:

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)

回答by wossoneri

try

尝试

[button setBackgroundImage:yourIconImage forState:UIControlStateDisabled];
[button setEnabled:NO]

回答by MH175

The accepted answer does not work for me (I think it's because I'm using an image, not text).

接受的答案对我不起作用(我认为这是因为我使用的是图像,而不是文本)。

You can initialize a UIBarButtonItem using a custom view, so in order to solve the issue I subclassed UIBarButtonItem so that it takes a UIButton as an initializer argument.

您可以使用自定义视图初始化 UIBarButtonItem,因此为了解决这个问题,我将 UIBarButtonItem 子类化,以便它采用 UIButton 作为初始化参数。

Here is the code.

这是代码。

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)            
    }

}

And I used it like this in my ViewController:

我在我的 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
}

I got the frame dimensions using the method here: https://stackoverflow.com/a/45374012/6167296

我使用这里的方法获得了框架尺寸:https: //stackoverflow.com/a/45374012/6167296

You will also have to set the target-actions on the button itself. You can see a similar answer here: https://stackoverflow.com/a/2796488/6167296

您还必须在按钮本身上设置目标操作。你可以在这里看到类似的答案:https: //stackoverflow.com/a/2796488/6167296

回答by Albert Bori

To set the color for disabled state across the entire app, you can do the following once in your AppDelegate's application:didFinishLaunchingWithOptions::

要在整个应用程序中设置禁用状态的颜色,您可以在AppDelegate's 中执行以下操作application:didFinishLaunchingWithOptions:

let attributes: [String: Any] = [
    NSForegroundColorAttributeName: UIColor.red.withAlphaComponent(0.5)
]

UIBarButtonItem.barAppearanceWhenContained(in: UINavigationBar.self)
    .setTitleTextAttributes(attributes, for: UIControlState.disabled)

回答by Unis Barakat

For swift 4.X

对于 swift 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)

basically NSForegroundColorAttributeNamebecame NSAttributedStringKey.foregroundColorand states went from being caps to lower case .Disabledbecame .disabled, etc..

基本上NSForegroundColorAttributeName变成了NSAttributedStringKey.foregroundColor,状态从大写变成了小写.Disabled变成了.disabled等等。

回答by Kevin Singh

SWIFT 5

快速 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)

回答by MattyG

myBarButtonItem.setBackgroundImage(myIconImage, forState: .Normal, barMetrics: .Default)
myBarButtonItem.setBackgroundImage(myIconImage, forState: .Disabled, barMetrics: .Default)
myBarButtonItem.enabled = false