简体   繁体   中英

Setting Size of UIButton Independent of Content

I'm creating a row of UIButtons with borders, whose titles will be set to different numbers. As it is, the buttons all resize themselves according to the length of the text inside, but I'd like them to all be the same size, a bit larger than is needed to accomodate the text, as this will look better.

I added borders to the buttons using a subclass in which I specify the following:

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

Is there a way I can specify in absolute terms the size of the rectangle that defines the border around the button? I've tried setting self.frame to a CGRect, but this doesn't seem to affect the border at all.

Are you using storyboards?

If so ctrl and drag your button to the other button you want to be equal widths.

If you aren't using storyboards:

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

Let me know if you have any trouble

try titleEdgeInsets property of uibutton. doc is given here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/titleEdgeInsets

default is zero which means it will just fit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM