I have set the UIButton
's background image and put a title on it(I used setBackgroundImage
method not setImage
). Now I want to expand the hitTest area of a UIButton
without extrude it's background image.
How can I do this?
A cleaner way to do this is to override pointInside.
Here's a Swift version:
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let expandedBounds = CGRectInset(self.bounds, -15, -15)
return CGRectContainsPoint(expandedBounds, point)
}
Here's a corrected version of the accepted answer. We're using bounds
instead of frame
and CGRectInset
instead of CGRectMake
. Cleaner and more reliable.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return CGRectContainsPoint([self expandedBounds], point) ? self : nil;
}
- (CGRect)expandedBounds {
return CGRectInset(self.bounds, -20, -20);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With