IPhone UIButton with UISwitch functionality

Is there a way to implement UISwitch with custom graphics for switch states? Or as an alternative, on the contrary, UIButton with UISwitch functionality?

+55
iphone uibutton uiswitch
Feb 12 2018-10-12
source share
4 answers

UIButton already supports switch functionality.

Just set a different image in Interface Builder for "Configuring Selected States" and use the selected property for UIButton to switch its state.

+97
Feb 12 2018-10-21
source share

set the image in the selected state:

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

and then touch the internal selector, set:

 button.selected = YES; 

if you want this to deselect another button, set:

 otherButton.selected = NO; 
+12
Apr 05 2018-12-12T00: 00Z
source share

To build on what PGB and nurne said above, after you set your states and add a selector (event method), you would like to put this code in this selector.

 - (IBAction)cost:(id)sender { //Toggle current state and save self.buttonTest.selected = !self.buttonTest.selected; /** The rest of your method goes here. */ } 
+7
Apr 15 '13 at 10:12
source share

For software-inclined:

 -(void) addToggleButton { CGRect aframe = CGRectMake(0,0,100,100); UIImage *selectedImage = [UIImage imageNamed:@"selected"]; UIImage *unselectedImage = [UIImage imageNamed:@"unselected"]; self.toggleUIButton = [[UIButton alloc] initWithFrame:aframe]; [self.toggleUIButton setImage:unselectedImage forState:UIControlStateNormal]; [self.toggleUIButton setImage:selectedImage forState:UIControlStateSelected]; [self.toggleUIButton addTarget:self action:@selector(clickToggle:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.toggleUIButton]; } -(void) clickToggle:(id) sender { BOOL isSelected = [(UIButton *)sender isSelected]; [(UIButton *) sender setSelected:!isSelected]; } 
+3
Feb 10 '14 at 14:14
source share



All Articles