How to remove UIButton top and bottom padding when you create it using auto layout?

When creating a UIButton with automatic layout, intrinsicContentSize always contains different top and bottom additions depending on the font size of the text. I am trying to set contentEdgeInsets , but it does not work for top / bottom fill.

How to fix indentation to 0 or any constant value?

+8
ios objective-c padding autolayout uibutton
source share
3 answers

After some experimentation, it seems that if you try to set contentEdgeInsets to all zeros, the default inserts are used. However, if you set them to almost zero, it works:

 button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0.01, bottom: 0.01, right: 0) 

It also appears that the values ​​get floor 'd, so you really won't get a fractional addition.

+13
source share

If you want the button to have size in its titleLabel content, I find that the only way to do this is to subclass UIButton and override intrinsicContentSize. Hope this works for you!

Worked for me on iOS 9.3

 class CustomButton: UIButton { override func intrinsicContentSize() -> CGSize { return titleLabel?.intrinsicContentSize() ?? super.intrinsicContentSize() } } 
+1
source share

See if this works. Create vertical constraints for the UIButton titleLabel instead of the button itself.

-one
source share

All Articles