8

I am attempting to increase the hit area of my UIButton. I have a new Xcode project with a UIButton in the center of the storyboard. The button is connected to my view controller with an outlet and action. I'm programmatically able to change the button title and just for fun made it's action change the background of the view when I tap the button. So I know everything is connected and works.

From what I've read online, this should work but it's not.

[button setFrame: CGRectMake(button.frame.origin.x, button.frame.origin.y, -64, -64)];

Any thoughts? I've tried without the negative margins and that doesn't work either. BTW, I have tried the many places this question is asked but it's not working. Thank you for your help. Also, I've heard subclassing UIButton could create an issue, any thoughts on that?

Update: The UIButton I want to make has a background color and is a specific size. I want to keep that size the same but increase the hit area around it without distorting the look of the button.

Jim Bak
  • 638
  • 1
  • 8
  • 17
  • 2
    Can't you just click and drag the corners of the UIButton within the storyboard to increase the size? – user3746428 Oct 06 '14 at 21:52
  • @user3746428 Increasing the button size also increases the background color I've set for the button. I want to keep the button looking the exact same it does now but increase the hit area around it. Thank you for mentioning this, I'll update my question. – Jim Bak Oct 06 '14 at 21:54
  • @JimBak you could create a UITapGestureRecognizer on the UIView and determine if the tap is near the button. – Wyetro Oct 06 '14 at 21:59
  • @JimBak did you find a solution? – Crashalot Apr 06 '16 at 17:34

3 Answers3

5

i think you just want to use the same button for restricting the background color or back ground image to the same area and increase the touch area?

as per my knowledge you cannot do that in interface builder. But thru coding you can by using layers.

take outlet of the button

  CALayer *buttonlayer=[CALayer layer];

  buttonlayer.frame=CGRectMake(50 , 50, 50, 50); // set the frame where you want to see the background color or background image to be visible in your button

  buttonlayer.backgroundColor=[[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourimage.png"] ] CGColor]; // if u want image (but remember the image size must same as your layer frame it may  look bad if the image bounds does not mach your layer bounds, because you are setting color not image)


  // to set color (donot use if you want image, use colorWithPatternImage to set image color)
   buttonlayer.backgroundColor=[[UIColor graycolor] CGColor];  // if you want only color

    // you can add round rects to the button layer

    buttonlayer.cornerRadius=2.5f;

   [yourbutton.layer addSublayer:buttonlayer];

i am not sure what you are looking for i hope this help you but check the image onceenter image description here

Smile
  • 667
  • 1
  • 5
  • 21
2

To increase the hit area without distorting the background color or image, it would probably be easiest just to separate them. Have a clear UIButton, with no background color, above a UIView (or UIImage, etc) that provides the visuals. That way, you can do whatever you want to the frame of the UIButton, and it won't affect the visuals unless you apply the same transformations to the other UIView.

As for why that line of code doesn't work, the third and fourth elements in CGRectMake aren't margins, they're dimensions: you can't have a negative height and width. Put in the ultimate size you want it to be, not the change or the margins, (i.e. if you want to shrink a width of 100 by 20, input 80).

Nerrolken
  • 1,975
  • 3
  • 24
  • 53
  • 1
    Thank you for the clarification. I really don't want to put a button on top of an image and have to manage auto layout with that. I can't believe there's just no easy way to simple increase the hit area of a button by a certain number of pixels... – Jim Bak Oct 06 '14 at 22:42
2

Subclass UIButton and implement the backgroundRectForBounds method.

Expand the frame to your desired touch area size, then shrink the bounds of the background by adding insets.

- (CGRect)backgroundRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, -50, -50);
}

More info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uibutton_class/index.html#//apple_ref/occ/instm/UIButton/backgroundRectForBounds:

dfmuir
  • 2,048
  • 1
  • 17
  • 14