Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Abstract

The button is a treasure control that allows you to add images, text, and even change backgrounds to different states at the time of setting. Change to a different state display for different presentation scenarios. Exactly how to change the state was really confusing for me, so document the process. If you are not interested in understanding the process, you can directly slide to the bottom and save the screenshot form.

SetImage (, for:); setImage(, for:); setImage(, for:);

let btn = UIButton()
btn.setImage(UIImage(named: "pic1"), for: .normal)
btn.setImage(UIImage(named: "pic2"), for: .disabled)
Copy the code

After setting up the button control, all that is left to do is to set its state where it needs to display different states. Just as the button has a state property, btn.state =.

Error: State property can only be read, cannot be written.

var state: UIControl.State { get }
Copy the code

I’m completely confused at this point. I’m going to change the button state to.disabled.

IsSelected is true or false to change the status of the button. Disabled?

This…… Can not be set, can not change it, if so, open this attribute is to play? Do not give up, this path is not going to go, change the path, look at the.disabled source code.

Originally, unexpectedly, out of my expectation. IsEnabled is used to change the status. Now that you are here, you can summarize the status and changes of the button:

UIControl.state How change? State to explain
normal The following propertiesBoolValue of allfalse Default, enabled but not selected or highlighted
highlighted isHighlighted The highlighted
disabled isEnabled disable
selected isSelected The selected
focused The unknown Focusing on the
application The unknown Control to attach the application identity
reserved The unknown Reserved identifier inside the frame

At this point, what state to set for the button, how to change the state directly refer to the above table. All these unknowns, let’s see if we find them later.

digression

Time is short and what you said may not be comprehensive. If you have any problems during your review, please leave a message in the comment section and I will reply as soon as possible