I'm currently developing a simple iOS app using Xcode and Swift. In my app, there is a button that some times is disabled/enabled, depending on something else the user have touched. But when I set "button.enabled = false", I also what the button to grey out, so that the user knows the button is currently disabled. How could this be done?
4 Answers
Use following code for customizing button's title for disabled state. You can call it inside viewDidLoad
:
button.setTitleColor(UIColor.grayColor(), forState: .Disabled)
If you would like to customize a background colour for the disabled button, use approach from this answer: How to change background color of UIButton when it's highlighted
Swift 5.3:
button.setTitleColor(.systemGray, for: .disabled)

- 353
- 1
- 6
- 12

- 630
- 5
- 14
-
check my answer in https://stackoverflow.com/questions/5715092/disabled-uibutton-not-faded-or-grey/46828678#46828678 – iosMentalist Oct 19 '17 at 11:23
You can do this via Storyboard.
Select the button, go to identity inspector and click on state configuration and choose "disable" state.
Then set the background color of the button to gray color.
From here you can customize your UIButton according to your requirements in different states and all you need is to manage setting the state of the button in your code.

- 2,187
- 1
- 17
- 30
Swift 3
As accepted answer, but updated for Swift3
button.setTitleColor(UIColor.gray, for: .disabled)

- 2,367
- 3
- 18
- 23
Standard buttons change their UI when you set the disabled flag to true.
If that doesn't work, you can use slashdot's gray title color change, or do what I often do, which is to set the alpha on the button to .6 or so. (If you have non-uniform contents behind the button, however, this isn't a good choice because whatever's behind the button will show through.)

- 128,072
- 22
- 173
- 272