Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UIButton state programmatically in Swift

I develop app for drawing on screen. I have toolpanel with bunch of buttons. Some of them have to become disabled or pressed depending on current user action. How can I change UIControlState of buttons programmatically considering that "state" property is readonly? If it's impossible, what is alternative for this purpose?

like image 311
Oleh Liskovych Avatar asked Apr 13 '17 14:04

Oleh Liskovych


2 Answers

I think you're confused. UIControlState is used to set up target/actions, or to change the appearance of the button for specific states (So, for example, you can specify the image that's used when it's selected, and a different image for when the button is not in the selected state.)

If you want to change the state to selected, you just set the selected property.

button.isSelected = true

Likewise for isEnabled:

button.isEnabled = false //disable the button
like image 198
Duncan C Avatar answered Nov 03 '22 23:11

Duncan C


Use the correct property to change the state. For example

button.isSelected = true

or

button.isHighlighted = true
like image 27
Retterdesdialogs Avatar answered Nov 04 '22 00:11

Retterdesdialogs