pointInside:withEvent: in a UIButton category using associated objects to define negative edge insets, or adjust the image edge insets in Interface Builder. Both methods enlarge the tappable region without altering the button’s visible frame.Why this happens
UIButton’s default hit-test area matches its bounds rectangle. For small controls — such as the system Info Dark button — that rectangle can fall below Apple’s recommended 44 × 44 pt minimum touch target. Users with larger fingers or motor impairments then miss taps, leading to frustration and failed interactions.
Interface Builder’s Image Insets fields only shift the image inside the existing bounds; they do not grow the hit-test region. To truly enlarge the tappable area you must either customize hit-testing logic or use layout constraints that give the button a larger intrinsic content size.
How to fix it
1. Reusable category with negative edge insets (recommended)
Create a category that adds a hitTestEdgeInsets property. Negative insets expand the hit rectangle outward; positive insets shrink it. The implementation uses associated objects so no subclassing is required.
UIButton+HitTest.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (HitTest)
@property (nonatomic, assign) UIEdgeInsets hitTestEdgeInsets;
@end
NS_ASSUME_NONNULL_END
UIButton+HitTest.m
#import "UIButton+HitTest.h"
#import <objc/runtime.h>
@implementation UIButton (HitTest)
static const void *HitTestEdgeInsetsKey = &HitTestEdgeInsetsKey;
- (void)setHitTestEdgeInsets:(UIEdgeInsets)insets {
NSValue *value = [NSValue value:&insets withObjCType:@encode(UIEdgeInsets)];
objc_setAssociatedObject(self, HitTestEdgeInsetsKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIEdgeInsets)hitTestEdgeInsets {
NSValue *value = objc_getAssociatedObject(self, HitTestEdgeInsetsKey);
if (value) {
UIEdgeInsets insets;
[value getValue:&insets];
return insets;
}
return UIEdgeInsetsZero;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) ||
!self.isEnabled ||
self.isHidden) {
return [super pointInside:point withEvent:event];
}
CGRect expanded = UIEdgeInsetsInsetRect(self.bounds, self.hitTestEdgeInsets);
return CGRectContainsPoint(expanded, point);
}
@end
Usage (negative values grow the area):
#import "UIButton+HitTest.h"
// Expand 10 pt on every side
infoButton.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);
Swift projects: Wrap the category in a bridging header or port the logic to a Swift
extension UIButtonusingobjc_setAssociatedObject/objc_getAssociatedObject.
2. Quick Interface Builder workaround (image-based buttons only)
If the button displays only a background image and no title, you can cheat by increasing the image insets to push the image inward, then giving the button a larger frame in Auto Layout. The visual size stays the same because the image is centered, but the larger frame becomes the hit area.
- Select the button in the storyboard / xib.
- Open the Attributes Inspector → Image Insets.
- Set Top / Left / Bottom / Right to the desired expansion (e.g., 10).
- Add width/height constraints that are image size + 2 × inset.
This avoids code but couples layout to the image dimensions and doesn’t work for titled or system-style buttons.
How to prevent it
- Design buttons at ≥ 44 × 44 pt from the start (Human Interface Guidelines).
- Use standard system buttons (
UIButton.Configuration) which already meet the minimum. - Test with Accessibility Inspector → Touch Target overlay to catch undersized targets early.
- Prefer the category approach for consistent behavior across the codebase.
FAQ
Does the category work with SwiftUI’s UIViewRepresentable?
Yes. The wrapped UIButton instance behaves identically; just set hitTestEdgeInsets before returning the view from makeUIView.
Can I animate the hit-test expansion?
No. pointInside:withEvent: is called on every touch event and must return quickly. Changing hitTestEdgeInsets inside an animation block has no visual effect because the hit rectangle is not rendered.
What about contentEdgeInsets / titleEdgeInsets?
Those insets affect layout of the title and image inside the button’s bounds, not the hit-test area. They cannot make the tappable region larger than the button’s frame.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
