Reputation: 14427
I have a small custom view that I use as a button. I make this happen with a simple UIButtonTypeCustom
, which I include as a subview over the bounds of the view. Since the view is small, however, I want to increase the size of the hit target. This is easily done (one would think) by setting the frame of my button to be larger than the bounds of the custom view and setting clipsToBounds
to NO. This works for the visual (the custom button draws outside the parent view) but it does not receive touchUpInside
events on the area outside of the parent view. How do I make the button receive touchUpInside
events?
- (id) initWithFrame: (CGRect) frame {
if (self = [super initWithFrame: frame]) {
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = NO;
// ... various image views and labels ...
UIButton *touchArea = [UIButton buttonWithType: UIButtonTypeCustom];
touchArea.backgroundColor = [UIColor colorWithRed: 1 green: 0 blue: 0 alpha: .3]; // so I can see the bounds
CGRect touchAreaFrame = self.bounds;
CGFloat checkboxHitTargetExtension = 7;
touchAreaFrame.origin.y -= checkboxHitTargetExtension;
touchAreaFrame.size.height += (checkboxHitTargetExtension * 2);
touchArea.frame = touchAreaFrame;
[self addSubview: touchArea];
// ... add listeners etc ...
}
return self;
}
Upvotes: 0
Views: 1584
Reputation:
You can use pointInside:withEvent: to define whether or not a specific point is "inside" the View. See the UIView reference for more info.
Also checkout CGRectOffset its very handy for making a CGRect bigger.
Upvotes: 2
Reputation: 19323
Make the button not a child of the view but a sibling to the view, and positioned on top of it, and then there are no clipping issues on the touches to the button.
Upvotes: 0