UIButton Vertical Align Bottom Not Working


November 2018


217 time


The UIButton title vertical align bottom not working. The button is inside a table cell. I tried several methods.

  1. Inside xib, select button, on the right panel, click on vertical bottom alignment.

  2. Inside xib, select button, on the right panel, select title insets and give amount of title inset.

Although in storyboard, I can see the alignment align correctly on bottom of the button, however, when open in APP, it STILL align centre vertically.

I also try the following code:

// change to .alignBaseline still not working. same effect.
questTitleButton.titleLabel?.baselineAdjustment = .none 
questTitleButton.titleLabel?.numberOfLines = 1
questTitleButton.contentVerticalAlignment = .bottom
questTitleButton.contentHorizontalAlignment = .left

All above method DOES NOT WORK. The button is still vertically centred after running the app.

My Solution

The only way I can make it work is to set a top inset in content insets area.

enter image description here

Now my question is why? I read many posts, but no one have the same problem as I have.

Such as this, or this. They all have problem with align text but I tried their solutions, only setting content insets works.

So why I can only align text to bottom of button by setting content insets? Did I miss anything here? Did I do something wrong when setting up the button? I use pure autolayout and create the view in .xib file.

The button do have a height constraint and there is no constraint conflict or error. Everything seems to be fine.

0 answers