4

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.

nuynait
  • 1,912
  • 20
  • 27

1 Answers1

6

well, I've just met exactly the same problem. Only x and y constraints were added to the button, and the content held on to its center stubbornly, even though its content has already aligned to bottom showed in storyboard. Finally I figured out a solution, switch to size inspector, and add 1 in content insets' top value, then it all went well.

LiLi Kazine
  • 195
  • 2
  • 9