Uibutton: Making the Hit Area Larger Than the Default Hit Area

Making the UIButton hit area larger than the default hit area

Why did you simple create a custom button which are at the size you want for your hit box but include a margin inside it's content ?

Or may be with a UIEdgeInsets put on your button, your button will have a margin so the hit box seems bigger than button with this solution..

How can I increase the Tap Area for UIButton?

You can simply adjust the content inset of the button to get your desired size. In code, it will look like this:

button.contentEdgeInsets = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
//Or if you specifically want to adjust around the image, instead use button.imageEdgeInsets

In interface builder, it will look like this:

interface builder

UIButton hit area is too big

Although I still have no idea what's causing this bug. (maybe a bug in the iOS7 sdk...?) I fixed it by subclassing UIButton and doing a hit test to check if I'm really clicking the button.

    #import "TTButton.h"

@implementation TTButton {}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint([self bounds], point)) {
NSLog(@"inside");
return self;
}

return nil;
}

@end

How to increase selection area of UIButton?

You can achieve this by setting the button's contentEdgeInsets, for example:

toggleButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0); //set as you require 


Related Topics



Leave a reply



Submit