设置UIButton的大小与内容无关

时间:2015-11-06 20:22:11

标签: ios swift

我正在创建一行带边框的UIButtons,其标题将设置为不同的数字。实际上,按钮都根据文本内部的长度自行调整大小,但是我希望它们都大小相同,比容纳文本所需的大一点,因为这看起来会更好。

我使用子类为按钮添加了边框,我在其中指定了以下内容:

self.layer.cornerRadius = 5.0;
self.layer.borderColor = UIColor.blackColor().CGColor
self.layer.borderWidth = 1
self.backgroundColor = UIColor.whiteColor()
self.tintColor = UIColor.blackColor()

有没有办法可以用绝对术语指定定义按钮边框的矩形的大小?我曾尝试将自我框架设置为CGRect,但这似乎根本不会影响边框。

2 个答案:

答案 0 :(得分:0)

您使用的是故事板吗?

如果是这样,按住Ctrl键并将按钮拖动到另一个按钮,你想要宽度相等。

如果您没有使用故事板:

constraint = [NSLayoutConstraint
    constraintWithItem:myButton
        attribute:NSLayoutAttributeWidth
        relatedBy:NSLayoutRelationEqual
          toItem: otherButton
       attribute:NSLayoutAttributeWidth
      multiplier:1.0f
        constant:0];

如果您有任何问题,请告诉我

答案 1 :(得分:0)

尝试uibutton的titleEdgeInsets属性。 doc在这里给出:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/titleEdgeInsets

默认值为零,这意味着它只适合