Reputation: 725
I use a small icon as an image of a UIbutton. I want to increase the hit area of the icon so the IBaction would be called without the need to click exactly at the small icon. I found similar question here and the solution was to adjust the inset, but this doesn't seem to work for me, the hit area is still the same.
smallButton.imageEdgeInsets = UIEdgeInsets(top: -100.0, left: -100.0,bottom: -100.0, right: -100.0)
smallButton.contentEdgeInsets = UIEdgeInsets(top: -100.0, left: -100.0, bottom: -100.0, right: -100.0)
Upvotes: 1
Views: 1283
Reputation: 2098
A simple but working way is set a button above the little image like this.
Upvotes: 0
Reputation:
Increase the height and width of the button. If that doesn't work for you.
You can either make the same button for it that could be clear color with no title and has twice size as that of the button previous one. Then connect that button with IBAction of the previous.
Note: Make sure that the button new one is in the front of the previous button.
Upvotes: 0
Reputation: 2099
You could subclass UIButton or a custom UIView and override point(inside:with:) with something like:
Swift 3
override func point(inside point: CGPoint, with _: UIEvent?) -> Bool {
let margin: CGFloat = 5
let area = self.bounds.insetBy(dx: -margin, dy: -margin)
return area.contains(point)
}
Upvotes: 3