hacker
hacker

Reputation: 8957

limiting the touchable area in a UIButton in IPhone?

I have three custom buttons with non-rectangular images close to each other in my view. Then I have a problem with touchable area's of each button overlap with other buttons. So how can I limit the touchable area of each buttons to get the corresponding actions?

Upvotes: 5

Views: 1205

Answers (3)

N C
N C

Reputation: 539

You should create custom Type Buttons and add required images on each using this code:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[btn setFrame:frame];

Creating buttons with images this way will not result in overlapping images issues !!

Upvotes: 3

Peter DeWeese
Peter DeWeese

Reputation: 18343

Use two components. A UIImageView with a smaller UIButton on top.

Upvotes: 3

vikingosegundo
vikingosegundo

Reputation: 52237

You can overwrite -pointInside:withEvent:, that internally will be used for hit testing.

A nice project using this technique is OBShapedButton, where transparent pixel will not trigger a hit.

Upvotes: 6

Related Questions