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.
Related
I've created a custom button and set two images, one is for normal, and the other is for the selected mode. But the voice-over always says the normal image name text when the button is not selected. I've tried a lot but could not disable it.
When I disable the button imageView accessibility it is not working.
button.imageView?.isAccessibilityElement = false
When I disable the button accessibility, the voice-over is not working in accessibility mode.
button.isAccessibilityElement = false
If I remove the '.normal' mode image then it works, but normal mode image functionality is not considered/worked there. I'm surfing a lot. Help anyone and thanks in advance.
Code:
self.setImage(UIImage.init(named: imageName1), for: .normal)
self.setImage(UIImage.init(named: imageName1), for: .selected)
Simply, It is not possible. You can use an accessibility label instead.
button1.imageView?.accessibilityLabel = "Radio button deselected"
You have to set button accessibilityLabel as empty string.
button1.accessibilityLabel = ""
VoiceOver will stop saying image name for any of the state.
In TableViewCell, I have custom imageview which act as a radio button. This imageview has two states
Selected
Not Selected
By default I have given, Accessibility label as "Selected Checkbox" and "checkbox". Now I want to speak voice text as "new item selected" when it selects and "item deselected" when deselected.
Can we give all four different label? How can I get the same.
Updated: I tried using
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification,#"text")
but it skips the text which need to speak.
I'm not entirely sure what you are attempting to do here but I'll try to provide an alternative way of making this accessible.
Instead of treating each radio button as either selected or deselected would it not be better to treat the entire group of radio buttons as one combined accessibility element.
So if you have radio buttons for [cat, dog, rabbit, guinea pig]. Then your accessibility should read something like...
Animal selection group: none selected
or
Animal selection group: rabbit selected
etc...
Having said all of that... what is this UI you are trying to create?
Radio buttons are fundamentally not a part of iOS. It would be a much better alternative to use UI that users know. And then by doing this you make your accessibility issue a non-issue.
Perhaps a UIPickerView or a UITableView might be better alternatives to radio buttons?
Not sure what are you exactly want to do.
My assumption: You want to generate sound on click of a button for tableviewcell and also changing the image of Imageview at the same time while checking the state of it (selected or non-selected).
***** do it inside your customTableViewCell class...
create IBOutlets in .h or .m for your 3 items from customtableviewcell xib.
create boolean flag to maintain current state (selected or non selected) of that cell.
create IBaction selector(method) to get event(touch up inside) of button click. Inside this method write code which checks following.
BOOL selectState; //Make this Global in classfile
if(selectState) // selected state YES
{
xyzImgView.image = //Your Non selection Image;
selectState = !selectState;
//Play your sound for Non selection
}
else // selected state NO
{
xyzImgView.image = //Your selection Image;
selectState = !selectState;
//Play your sound for selection
}
// Considering you have an IBOutlet to checkBoxButton
checkBoxButton.accessibilityLabel = checkBoxButton.isSelected ? "selected" : "not selected"
checkBoxButton.accessibilityHint = checkBoxButton.isSelected ? "" : "Tap to select"
I'm trying to create a rather custom segmentControl. What is the easiest way to create such below. with just text and a border line the selected?
You can create buttons and give tag to each of them,which will identify which button has been selected.Whenever a button is marked as selected change the state to .selected with its image.
Choice -1
ya just created 4 buttons and create the one common method , inside the method assign the tag for each button for identify which button is selected , on that selected button change the TextColor and use use Underline,else button are another color
Choice -2
ya just created 4 buttons and create the one common method , inside the method assign the tag for each button for identify which button is selected , on that selected button change the Image in UIControlStateNormal else no selection Button use normal Text or another Image
You can make fastens and offer tag to each of them,which will recognize which catch has been selected. Whenever a catch is set apart as chosen change the state to .selected with its image.
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)
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.