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?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With