Xcode iOS Extends UIButton Attack Area

I am trying to increase the scope of my UIButton. I have a new Xcode project with UIButton in the storyboard center. The button is connected to the view controller with output and action. I can programmatically change the name of the button and just for fun made it change the background of the view when I press the button. Therefore, I know that everything is connected and working.

From what I read on the Internet, this should work, but it is not.

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

Any thoughts? I tried without negative fields, and this also does not work. By the way, I tried many places that this question was asked for, but it does not work. Thanks for the help. Also, I heard that UIButton subclasses can create a problem, any thoughts on this?

Update: The UIButton I want to make has a background color and has a specific size. I want to keep this size the same, but increase the area of ​​impact around it without distorting the appearance of the button.

+6
source share
3 answers

I think you just want to use the same button to limit the background color or back image in the same area and increase the touch area?

according to my knowledge, you cannot do this in the interface builder. But through coding you can using layers.

take the exit 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'm not sure what you are looking for. I hope this help helps you, but check the image once enter image description here

+5
source

To increase the area of ​​impact without distorting the background color or image, it would be easiest to simply separate them. Have a clear UIButton, with no background color, above a UIView (or UIImage, etc.) that provides visual effects. This way, you can do whatever you want in the UIButton frame, and this will not affect the visual effects unless you apply the same transformations to another UIView.

As for why this line of code does not work, the third and fourth elements in CGRectMake are not fields, they are dimensions: you cannot have a negative height and width. Put the maximum size you want, not change or margins (i.e. if you want to reduce width 100 by 20, input 80).

+2
source

Subclass UIButton and implement the backgroundRectForBounds method.

Expand the border to the desired size of the touch area, and then reduce the background border by adding inserts.

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

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

+2
source

Source: https://habr.com/ru/post/976327/


All Articles