Why UIButton can't stay selected state when disable? - ios

I have a UIButton which is set different images for normal and selected state.
I find that when I disable the button by setting button.enable = NO, even previous is selected state, the button will turn to normal state image, But I po the button selected value which is still YES.
How can I keep using selected image when button is disable? Is that the only way by changing normal state image when button change enable value?

Set the image for the case when button is Selected and Disabled as this is a different state that the solely other state. Set the image but for state like this:
UIControlStateSelected | UIControlStateDisabled
Control's state can be a combination of a few states from the list :)
As a nice lesson you can log changes of a control's state and see combinations of states it will gets in different cases.

In Swift:
let state = UIControl.State.selected.union(UIControl.State.disabled)

Related

Make a button appear selected

Is there a simple way of making a button appear selected with Swift? I currently have a series of buttons with background images. I want each of the buttons to appear 'selected' once tapped by the user; each button will be used to specify which options the user requires.
When you select your button you could change it's state to selected:
func buttonTapped(button: UIButton) {
button.selected = !button.selected
...
}
And if you want, you could change the background image of your UIButton to have a different image when it's state is selected like this:
button.setBackgroundImage(selectedImage, forState: .Selected)
This can also be done in Interface Builder:
You can make a button appear selected several ways. One is to have a second image which is slightly darker (i.e. looks depressed) and when you set the button's state to button.selected = true then the system will automatically use this second image for it's selected state. If you don't have a separate asset (which I wold recommend you do) then you can set button.userInteractionEnabled = false which will make it unable to be acted on by the user, as well as change it's alpha component or color or some other property to enforce to the user that the button is no longer selectable.

UIButton Image Not Changing With Interface Builder Settings (Selected & Disabled)

I am trying to set the toggle state of a UIButton through the interface builder. I have done it successfully using code, but I need to get it working through the interface builder.
Im not sure what the issue is but for selected I set its image to 'flashOn' and for disabled I set it to 'flashOff'
When I hold down the image, it shows the 2nd image, but pressing the image does not toggle between these two images.
Im certain it's super simple so if you can suggest what I am missing I would appreciate it
A button's selected and disabled states need to be set in code.
Can you show your implementation for when you got desired results through code? I think, from what I can piece together from your question, that you have mixed up the states of the button.
Default - images will display for this button in any state unless another is specified explicitely
Disabled - image will display when button is disabled. (button will not switch from this state through user interaction as it is disabled)
Selected - image will display when the button is selected, which as far as I know is only when you set the button as selected through code.
Highlighted - image will display when you press and hold down on the button

UIButton doesn't want to keep in Selected State when clicking on the button

I am trying to figure out what's wrong here, because as you can in the screenshots a different image is given for both, selected and default state.
The problem :
when I run on simulator, I click on the button, it shows the image of highlited state, and then back to the normal state without keeping changed at the selected state ! any hints or ideas about this particular issue ?
You should manually set selected state to YES in code when the button is pressed
- (IBAction)btnTapped:(UIButton*)button {
button.selected = !button.selected; // to switch from selected to unselected and vice versa
}
I think you should add a target to this button and in that method, you should write
button.selected=YES; //then only the image will change.

Highlighted state for UIButton doesn't come when going from selected to normal state

I have a UIButton , with 4 different states according to my app.
state-1 normal.
state-2 selected.
state-3 highlighted , going normal-to-selected.
state-4 highlighted , going selected-to-normal.
Initially button is in normal state, and when I press it it goes to selected state and keep toggling between these states.
this is achieved by specifying images for the two states and changing the selected property.
However, on change of every state I needed to change the highlighted state image to make sure it represents either of state 3 and state 4 while being pressed.
The problem is , while coming from selected to normal, it looks like there is not highlighted state for UIButton.
So for now I keep a BOOL ivar to check the selection state in my View.
and keep the button in normal state and change its image for highlighted state and normal state with every action based on value of the BOOL ivar.
Is there any simpler way to achieve this ?
The reason for this is that the state is a mix of two values.
Some of the most commons ones
UIControlStateNormal,
UIControlStateHighlighted,
UIControlStateDisabled,
UIControlStateSelected,
UIControlStateSelected | UIControlStateHighlighted,
UIControlStateSelected | UIControlStateDisabled,
So if you want to have a highlighted state when selected then I prefer to do like this.
[button setImage:imageHighlighted forState:UIControlStateSelected | UIControlStateHighlighted];
If a general rule is that you always have same highlighted state when selected you can do something along the lines of
UIControlState mixedState = UIControlStateSelected | UIControlStateHighlighted;
[button setImage:[button imageForState:state] forState:state];
[button setBackgroundImage:[button backgroundImageForState:state] forState:state];
[button setTitleColor:[button titleColorForState:state] forState:state];
And so on
Saving the highlighted state in a Boolean variable is a common way to keep track of the state, however here's something that will simply your code.
There is a setHighlighted method that gets called on the UIControl that you can override. You can set your variable in there instead of in multiple actions:
-(void)setHighligted:(BOOL) highlighted {
self.mySavedHighlightedState = highlighted;
}
Create a custom UIButton class with a mySavedHighlightedState property to implement this.

Deleselect uisegmentedcontrol selection after clicking on a save button

I'm using a 3 button UISegmented control for a choice selection. I also have a save button that retrieves the chosen control.
When the save button is clicked I want to have to UISegmentedcontrol cleared (ie the previous selected button unselected). I'm not looking for the setMomentary as I want the selection to stick but also be able to unselect it later.
[myUISegmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
myUISegmentedControl.selectedSegmentIndex = -1; //turn off the current selection
With Swift 4 and iOS 11, the Apple documentation states for selectedSegmentIndex:
The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.
Therefore you can use one of the two following implementations in order to remove the selection of your UISegmentedControl instance:
mySegmentedControl.selectedSegmentIndex = -1
mySegmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment

Resources