Search code examples
iosswiftuikit

UIButton.contentEdgeInsets gets ignored when UIButton is in a vertical StackView with a UILabel


I have a problem with my UIKit Swift App. When using a vertical StackView with a UIButton and a UILabel, the contentEdgeInsets gets ignored. This only happens when the UIButton is inside the Stackview with a UILabel. When I remove the UILabel or use the exact same button outside of the StackView it works perfectly fine. I created a example project to showcase the problem.

Screen of the example project using following snippet

exampleButtonInStackView.titleLabel?.adjustsFontSizeToFitWidth = true
exampleButtonInStackView.layer.cornerRadius = 30
exampleButtonInStackView.backgroundColor = UIColor.white

exampleButton.titleLabel?.adjustsFontSizeToFitWidth = true
exampleButton.layer.cornerRadius = 30
exampleButton.backgroundColor = UIColor.white

let insets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
exampleButtonInStackView.contentEdgeInsets = insets
exampleButton.contentEdgeInsets = insets

Solution

  • OK I found the problem thanks to DonMag's reply. The problem was the "Baseline Relative" property of the StackView. When this property is checked, the contentInsets gets ignored.