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

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

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

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

编辑:

我正在使用以下:

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

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


当前回答

斯威夫特是这样做的:

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)
    }
}

爱你斯威夫特

其他回答

这个线程有点老了,但我自己也遇到了这个问题,并且能够通过使用负插入来解决它。例如,在这里替换你想要的填充值:

UIButton* myButton = [[UIButton alloc] init];
// setup some autolayout constraints here
myButton.titleEdgeInsets = UIEdgeInsetsMake(-desiredBottomPadding,
                                            -desiredRightPadding,
                                            -desiredTopPadding,
                                            -desiredLeftPadding);

结合正确的自动布局约束,您最终会得到一个自动调整大小的按钮,其中包含图像和文本!如下所示,将desiredleftpa添加为10。

您可以看到按钮的实际帧并没有包含标签(因为标签向右移动了10个点,超出了边界),但是我们在文本和图片之间实现了10个点的填充。

我想在我的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)

    }

}

您可以解决这个问题,而不必重写任何方法或设置任意宽度约束。你可以在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)

你没有指定如何设置insets,所以我猜你在使用titleEdgeInsets,因为我看到你得到了相同的效果。如果我使用contenttedgeinsets代替它正常工作。

- (IBAction)ChangeTitle:(UIButton *)sender {
    self.button.contentEdgeInsets = UIEdgeInsetsMake(0,20,0,20);
    [self.button setTitle:@"Long Long Title" forState:UIControlStateNormal];
}