Vertical layout an UIButton

If you’re an iOS developer, certainly you know that UIButton doesn’t layout its content vertically, it’s horizontally layout. So what will you do if you want to make an UIButton like this one:

Screen Shot 2017-05-06 at 3.07.44 PM

You have some solutions to do it: use image, add an UILabel or set inset of the title and image. But there’s a simple way to do it with a category of UIButton.

@interface UIButton (VerticalLayout)

- (void)centerVerticallyWithPadding:(float)padding;
- (void)centerVertically;


@implementation UIButton (VerticalLayout)

- (void)centerVerticallyWithPadding:(float)padding
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = [self.titleLabel.text sizeWithAttributes: @{NSFontAttributeName:self.titleLabel.font}];
    CGFloat totalHeight = (imageSize.height + titleSize.height + padding);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height),
                                            - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0f,
                                            - imageSize.width,
                                            - (totalHeight - titleSize.height),

- (void)centerVertically
    const CGFloat kDefaultPadding = 6.0f;
    [self centerVerticallyWithPadding:kDefaultPadding];


Link reference:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s