62

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?

scud
  • 1,147
  • 1
  • 14
  • 25
  • 6
    Whoever voted to close this question as an exact duplicate, you should at least provide a comment explaining which post it's a duplicate of. – Jasarien Feb 12 '10 at 23:38

4 Answers4

102

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.

pgb
  • 24,813
  • 12
  • 83
  • 113
  • It returns to normal after tapping/selecting. This is what I have done w/o IB. [favButton setImage:favOff forState:UIControlStateNormal]; [favButton setImage:favOn forState:UIControlStateHighlighted]; [favButton setImage:favOn forState:UIControlStateSelected]; [favButton setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:favButton]; – scud Feb 12 '10 at 21:24
  • The UISwitch nicely animates its state change. I wonder how to animate a UIButton changing state. Any ideas? – radven Dec 09 '10 at 22:15
  • 5
    How do you get it to stay in the selected state? I added a target for UIControlEventTouchUpInside and in the handler I setSelected to "!button.selected". It turns blue as long as I hold the button down, but as soon as I "touchup", it goes back to white. Or do I have to use images instead of the selected color? –  Apr 28 '11 at 03:18
  • 1
    In Interface Builder - make sure you set an image for the "Default" and "Selected State" of the button. – Keith Dec 24 '11 at 18:03
  • Hi, can this be retained even when exiting the app? or how? – Bazinga Jul 19 '12 at 08:02
  • You can save the state to the NSUserDefaults – pgb Jul 19 '12 at 14:54
  • 1
    How to enable UIButton switch functionality? WHen clicked it is selected and when not clicked it's not selected. – Anonymous White Oct 22 '12 at 01:09
  • 1
    Has anyone noticed that when deselecting the UIButton it changes state on touch down? Is there anyway to have this occur on Touch Up Inside? When selecting the button it occurs on Touch Up Inside. – atreat Nov 29 '13 at 20:58
13

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;
nurnachman
  • 4,468
  • 2
  • 37
  • 40
9

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

- (IBAction)cost:(id)sender 
{
    //Toggle current state and save
    self.buttonTest.selected = !self.buttonTest.selected;

    /**
     The rest of your method goes here.
     */
}
Shaolo
  • 1,180
  • 11
  • 13
3

For programmatically 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];
}
Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82