我的应用程序中有一个UIButton,它非常小。有时可能很难点击它。有没有办法在按钮周围有20px的边距,保持图像大小相同?这是我的代码:
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);
}
基本上,我想要一个更大的区域来点击,但保持按钮大小相同。
答案 0 :(得分:3)
尝试使用setImageEdgeInsets。检查一下。
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)];
答案 1 :(得分:1)
UIButton子类并覆盖backgroundRectForBounds:
(或contentRectForBounds:
或适用于您的用例的任何内容)。
答案 2 :(得分:0)
如果你的按钮背景颜色很清晰,你可以增加按钮的大小。我最近为一个带有小图片的删除按钮做了这个:
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);