扩大UIButton点击区域

当设计图上的给出按钮尺寸较小,我们将对应的资源文件放入UIButton中,比如只有12x12pt,在真机调试中会发现难以点到按钮。
这时候可以通过继承UIButton,重写pointInside方法,使得按钮热区不够44×44pt的自动缩放到44×44pt

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect bounds = self.bounds;
    //若原热区小于44x44,则放大热区,否则保持原大小不变
 CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
 CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
 bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
 return CGRectContainsPoint(bounds, point);
}

问题解决。

原文网址:http://itony.me/129.html

你可能感兴趣的:(扩大UIButton点击区域)