Search code examples
iosuibuttonalignmentautosize

UIButton - text moved down


While I am using this code

    self.layer.cornerRadius = self.bounds.size.width / 2
    self.layer.masksToBounds = true
    //
    self.titleLabel?.adjustsFontSizeToFitWidth = true
    self.titleLabel?.minimumScaleFactor = 0.5
    self.titleLabel?.baselineAdjustment = .AlignCenters
    self.titleLabel?.numberOfLines = 1
    //
    self.showsTouchWhenHighlighted = true

to autoshrink label/text that is part of UIButton. For smaller type of devices I got the result like this

enter image description here

Any idea?

EDIT: I did it again from scratch and the result is bit different. Since I use almost everything the same, I might guess this is due to the fact that this version 8 of xCode with SWIFT 2.3 can produce strange things.

enter image description here

Basically what I did is to keep the button resizing with aspect ratio 1:1 and set constraints with the width 414 in the IB. Smaller devices with width 320 are actually displaying the results bit down than centre depends on the size, bigger == more down. Strange. I didn't touch any insets btw. Default is 10 left and 10 right.


Solution

  • Exchanging this line actually works, strange

    self.titleLabel?.baselineAdjustment = .None