如何添加按钮的触摸区域


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];

我想将按钮的可点击区域增加到(0,150,screenwidth/2,30)。我已经尝试设置contentEdgeInsetsimageEdgeinsets,但两者都不起作用。

我该怎么做?

您需要设置图像之类

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

然后设置您想要的按钮框架

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

子类Uibutton或类别/扩展名,并以此方式使用:

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

最新更新