Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UIButton with UISwitch functionality

Is there either a way to implement UISwitch with custom graphics for the switch-states? Or as an alternative the other way round, an UIButton with UISwitch functionality?

like image 392
scud Avatar asked Feb 12 '10 21:02

scud


People also ask

How to work with UISwitch in swift?

The UISwitch class declares a property and a method to control its on/off state. When a person manipulates the switch control (“flips” it), it triggers the valueChanged event. You can customize the appearance of the switch by changing the color used to tint the switch when it's on or off.

What is Uibutton Swift?

A control that executes your custom code in response to user interactions.

How do I center a button in Swift?

We can center the button by using the following methods: text-align: center – By setting the value of text-align property of parent div tag to the center. margin: auto – By setting the value of margin property to auto.


2 Answers

UIButton already supports a "switch" functionality.

Just set a different image in Interface Builder for the "Selected State Configuration", and use the selected property of UIButton to toggle its state.

like image 98
pgb Avatar answered Sep 26 '22 02:09

pgb


set the image to show on selected state:

[button setImage:[UIImage imageNamed:@"btn_graphics"] forState:UIControlStateSelected]; 

and then on touch up inside selector, set:

button.selected = YES; 

if you want this to cancel another button's selection, set:

otherButton.selected = NO; 
like image 37
nurnachman Avatar answered Sep 23 '22 02:09

nurnachman