Waitak Jong
Waitak Jong

Reputation: 87

how to add the touch area of a button?

selectgroup = [UIButton buttonWithType:UIButtonTypeCustom];
selectgroup.frame  = CGRectMake(screenWidth/4-23/2, 158, 23, 23) ;
selectgroup.hidden = NO;
UIImage *selectgroupimg = [UIImage imageNamed:@"groupicon2.png"];
[selectgroup setBackgroundImage:selectgroupimg forState:UIControlStateNormal];
//[selectgroup setcontentEdgeInsets : UIEdgeInsetsMake(0, 150, screenWidht/2, 30)];
[selectgroup addTarget:self action:@selector(selectgroup:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectgroup];

I want to increase the clickable area of the button to (0,150,screenwidth/2,30). I have try to set contentEdgeInsets and imageEdgeinsets but both doesn't work.

How can I do that?

Upvotes: 1

Views: 155

Answers (2)

Jugal K Balara
Jugal K Balara

Reputation: 927

You need to set image like (Button property setImage and setBackgroundImage both are different)

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[buttonObject setImage:btnImage forState:UIControlStateNormal];forState:UIControlStateNormal];

then Set button frame you want

buttonObject.frame  = CGRectMake(0,150,screenwidth/2,30);

Upvotes: 1

MCMatan
MCMatan

Reputation: 8833

Subclass UIButton, Or category/extension,, and use it like this:

-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return CGRectContainsPoint(UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(-44, -44, -44, -44)), point);
}

Upvotes: 0

Related Questions