Reputation: 3818
I have a UIButton in my app, and it's pretty small. It can be quite hard to tap it sometimes. Is there a way to have a 20px margin around the button, keeping the image the same size? Here's my code:
self.helpButton= [UIButton buttonWithType:UIButtonTypeCustom];
[self.helpButton setImage:[UIImage imageNamed:@"Info Button.png"] forState:UIControlStateNormal];
[self.helpButton addTarget:self
action:@selector(showHelp)
forControlEvents:UIControlEventTouchUpInside];
if ([[ UIScreen mainScreen ] bounds ].size.height == 568 ) {
self.helpButton.frame = CGRectMake(280.0, 528.0, 20, 20);
} else {
self.helpButton.frame = CGRectMake(280.0, 440.0, 20, 20);
}
Basically, I want to have a larger area to tap, but keep the button the same size.
Upvotes: 2
Views: 9326
Reputation: 4558
If your button background colour is clear you can just increase the size of the button. I did this for a delete button with a small image recently:
UIImage *deleteImage = [UIImage imageNamed:@"btnDelete"];
CGSize btnSize = CGSizeMake(deleteImage.size.width*2, deleteImage.size.height*2);
self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteButton setImage:deleteImage forState:UIControlStateNormal];
_deleteButton.frame = CGRectMake(0,0,btnSize.width,btnSize.height);
Upvotes: 0
Reputation: 991
try with setImageEdgeInsets. check this.
if ([[ UIScreen mainScreen ] bounds ].size.height == 568 ) {
self.helpButton.frame = CGRectMake(280.0, 528.0, 40, 40);
} else {
self.helpButton.frame = CGRectMake(280.0, 440.0, 40, 40);
}
[self.helpButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
Upvotes: 3
Reputation: 535566
Subclass UIButton and override backgroundRectForBounds:
(or contentRectForBounds:
or whatever works for your use case).
Upvotes: 1