如果我有一个UIButton安排使用自动布局,它的大小调整很好地适合它的内容。

如果我设置一个图像为按钮。图像,内在的大小似乎也解释了这一点。

然而,如果我调整按钮的titleEdgeInsets,布局不会考虑这一点,而是截断按钮标题。

我如何确保按钮的内在宽度占了嵌入?

编辑:

我正在使用以下:

[self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];

目标是在图像和文本之间添加一些分隔。


当前回答

我想在我的UIButton图标和标签之间添加一个5pt的空间。我是这样做到的:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
// more button config etc
infoButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
infoButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);

contenttedgeinsets, titleEdgeInsets和imageEdgeInsets相互关联的方式需要从每个insets中进行一点给予和获取。因此,如果你在标题的左边添加了一些插入,你就必须在右边添加负插入,并在内容的右边提供更多的空间(通过正插入)。

通过添加一个右边的内容插入,以匹配标题插入的移位,我的文本不会超出按钮的界限。

其他回答

斯威夫特是这样做的:

extension UIButton {
    override open var intrinsicContentSize: CGSize {
        let intrinsicContentSize = super.intrinsicContentSize

        let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
        let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom

        return CGSize(width: adjustedWidth, height: adjustedHeight)
    }
}

爱你斯威夫特

该选项在接口构建器中也可用。请看插图。我将左右各设为3。效果非常好。

您可以解决这个问题,而不必重写任何方法或设置任意宽度约束。你可以在Interface Builder中完成这一切。

Intrinsic button width is derived from the title width plus the icon width plus the left and right content edge insets. If a button has both an image and text, they’re centered as a group, with no padding between. If you add a left content inset, it’s calculated relative to the text, not the text + icon. If you set a negative left image inset, the image is pulled out to the left but the overall button width is unaffected. If you set a negative left image inset, the actual layout uses half that value. So to get a -20 point left inset, you must use a -40 point left inset value in Interface Builder.

因此,您需要提供足够大的左侧内容插入,为所需的左侧插入和图标和文本之间的内部填充创造空间,然后通过将图标和文本之间的填充量增加一倍来将图标向左移动。结果是一个左右内容相同的按钮,文本和图标对作为一个组居中,它们之间有特定数量的填充。

一些示例值:

// Produces a button with the layout:
// |-20-icon-10-text-20-|
// AutoLayout intrinsic width works as you'd desire.
button.contentEdgeInsets = UIEdgeInsetsMake(10, 30, 10, 20)
button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)

我想在我的UIButton图标和标签之间添加一个5pt的空间。我是这样做到的:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
// more button config etc
infoButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
infoButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);

contenttedgeinsets, titleEdgeInsets和imageEdgeInsets相互关联的方式需要从每个insets中进行一点给予和获取。因此,如果你在标题的左边添加了一些插入,你就必须在右边添加负插入,并在内容的右边提供更多的空间(通过正插入)。

通过添加一个右边的内容插入,以匹配标题插入的移位,我的文本不会超出按钮的界限。

Swift 3基于pegpeg的答案:

extension UIButton {

    override open var intrinsicContentSize: CGSize {

        let intrinsicContentSize = super.intrinsicContentSize

        let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
        let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom

        return CGSize(width: adjustedWidth, height: adjustedHeight)

    }

}