當設計圖上的給出按鈕尺寸較小,我們將對應的資源文件放入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);
}
問題解決。