Skip to content

Instantly share code, notes, and snippets.

@KATT
Last active January 3, 2016 05:59
Show Gist options
  • Save KATT/8419195 to your computer and use it in GitHub Desktop.
Save KATT/8419195 to your computer and use it in GitHub Desktop.
Makes UIButtons tap-friendly by making the hitbox a minimum of 44x44pt.
//
// US2TapFriendlyButton.h
//
// Created by Alexander Johansson on 14/01/2014.
//
//
#import <UIKit/UIKit.h>
@interface US2TapFriendlyButton : UIButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
@end
//
// US2TapFriendlyButton.m
//
// Created by Alexander Johansson on 14/01/2014.
//
//
#import "US2TapFriendlyButton.h"
#define kMinHitBoxHeight 44
#define kMinHitBoxWidth 44
@implementation US2TapFriendlyButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if ([super pointInside:point withEvent:event])
{
return true;
}
CGFloat insetWidth = 0;
CGFloat insetHeight = 0;
CGFloat deltaWidth = self.bounds.size.width - kMinHitBoxWidth;
CGFloat deltaHeight = self.bounds.size.height - kMinHitBoxHeight;
if (deltaWidth < 0)
{
insetWidth = deltaWidth / 2;
}
if (deltaHeight < 0)
{
insetHeight = deltaHeight / 2;
}
UIEdgeInsets hitTestEdgeInsets = UIEdgeInsetsMake(insetHeight, insetWidth, insetHeight, insetWidth);
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment