ios - 带插图的居中 UIButton 图像

标签 ios objective-c uibutton uiedgeinsets

我想创建一个 UIButton,但点击区域比图像大。 (例如:40x40 按钮,但图像只有 20x20,居中)。

这就是 imageEdgeInsets 的用途吗?

我已经通过编程方式设置了它:(这是在包含我的按钮的 UIView 中)

- (void)awakeFromNib {
    [_plusButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [_plusButton setContentMode:UIViewContentModeCenter];
}

来自 Storyboard

enter image description here

但它们似乎都不起作用。

最佳答案

知道了 我认为您将图像设置为按钮的背景图像,将其设置为图像然后它永远不会拉伸(stretch)(保持其原始形状)并且 imageEdgeInsets 将对其起作用

喜欢:

enter image description here

关于ios - 带插图的居中 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23990815/

相关文章:

ios - Xcode 6 Storyboard导航栏宽度不是 iPhone 6 的全宽

ios - 如何在 iOS 上录制对话/电话?

ios - Xcode 7.0.1 - 无效的 Swift 支持 - cocoapods 如何修复?

iphone - 如何在核心数据堆栈中的客户端上使用 ABPeoplePickerNavigationController

ios - UIButton addTarget 是如何工作的?

ios - 为什么在 Xamarin IOS customrenderer 中没有准确检测到长按开始/结束?

ios - 检查应用程序是否已安装

ios - 在 UICollectionViewCell 中滚动时 MPMoviePlayerController 变为空白

objective-c - 在 Objective C 惰性实例化中,我们为什么不接触 setter?

ios - 如果我向下滚动或不向下滚动,我会在 UIScrollView 中获得与 UIButton 相同的位置