Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grey out a button to let the user know that it is currently disabled in iOS? [duplicate]

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?

like image 698
Thor Avatar asked Aug 15 '16 21:08

Thor


2 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)
like image 172
slashdot Avatar answered Oct 09 '22 03:10

slashdot


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. enter image description here

like image 34
Ishika Avatar answered Oct 09 '22 02:10

Ishika