2

I'd like to make the touch area around UISwitch larger by 10 points on each side. Looking at a few of the suggestions in a related post (UIButton: Making the hit area larger than the default hit area), I've tried to increase the frame around the UISwitch using the below approach, however it results in the entire UISwitch stretching to fill the new frame.

slide to begin arrow with padding

Is there a more reasonable way this can be done?

// Increase margin around switch based on width
const CGFloat desiredWidth = 260.0f;  // real width is 240
const CGFloat margin = 0.5f * (desiredWidth - self.beginSwitch.frame.size.width);

// Add margin on all four sides of the switch
CGRect newFrame = self.beginSwitch.frame;
newFrame.origin.x -= margin;
newFrame.origin.y -= margin;
newFrame.size.width  += 2.0f * margin;
newFrame.size.height += 2.0f * margin;

self.beginSwitch.frame = newFrame;
Community
  • 1
  • 1
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • Put an invisible button down on top of it, and when it is hit, set the button state of the actual switch – trumpetlicks Feb 01 '13 at 21:39
  • Maybe as a last resort that would work. The problem with that approach is the user won't be able to slide the switch like they'd expect. – Kyle Clegg Feb 01 '13 at 21:40
  • 1
    Good point, I guess I NEVER actually slide the switches, I always tap them LOL – trumpetlicks Feb 01 '13 at 21:44
  • Yeah me either usually. I specifically made this switch wider and am letting the user "slide" to begin their session. They can also tap, but I'd love to preserve the slide functionality. – Kyle Clegg Feb 01 '13 at 21:47
  • so put four invisible buttons, one on each side :-) (or just one, behind the switch) – AlexChaffee Feb 01 '13 at 21:50

1 Answers1

0

I have accomplished such feats by putting an invisible button (with your larger size) in front of an image (or in your case the switch). Doing this you can SET the state of the switch and based upon the inverse of what the current state of the switch is. Also perform your actual action within the invisible button' action code.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33