Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode iOS increase UIButton hit area

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.

like image 355
Jim Bak Avatar asked Oct 06 '14 21:10

Jim Bak


People also ask

How do you make a UIButton multiline?

To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .

How do you center an image in UIButton?

You must set the content mode for the image inside the UIButton. The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton .

What is UIButton Swift?

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


3 Answers

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

like image 111
Smile Avatar answered Oct 13 '22 01:10

Smile


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).

like image 42
Nerrolken Avatar answered Oct 13 '22 01:10

Nerrolken


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:

like image 29
dfmuir Avatar answered Oct 13 '22 00:10

dfmuir