Skip to content

Instantly share code, notes, and snippets.

@cyrilchandelier
Created September 23, 2019 07:30
Show Gist options
  • Save cyrilchandelier/6927afab653608e536a17b44ffc04d07 to your computer and use it in GitHub Desktop.
Save cyrilchandelier/6927afab653608e536a17b44ffc04d07 to your computer and use it in GitHub Desktop.
A Swift UIButton extension to ensure the hit-zone is at least 44x44
import UIKit
class MinimumTouchAreaButton: UIButton {
override func hitTest(_ point: CGPoint, with _: UIEvent?) -> UIView? {
guard !isHidden, isUserInteractionEnabled, alpha > 0 else {
return nil
}
let expandedBounds = bounds.insetBy(dx: min(bounds.width - 44.0, 0), dy: min(bounds.height - 44.0, 0))
return expandedBounds.contains(point) ? self : nil
}
}
@ovidiulaz7
Copy link

I guess you should apply inset divided by two to get a 44 x 44 square

let expandedBounds = bounds.insetBy(dx: min(bounds.width - 44.0, 0) / 2, dy: min(bounds.height - 44.0, 0) / 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment