UIButton
title label has top and bottom padding, I want to remove padding.
Set UIButton
content mode did not work.
Here is my code
lazy var button: UIButton = {
let button = UIButton(type: .custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(ThemeColor.red, for: .normal)
button.setTitle("Push", for: .normal)
button.addTarget(self, action: #selector(buttonDidTap), for: .touchUpInside)
button.backgroundColor = .blue
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.contentMode = .scaleAspectFill
return button
}()
How can I remove the padding space!
3
Answers
As Matt pointed out, you can fix this by adjusting the button’s
contentEdgeInsets
However, one thing I noticed, if you set the
contentEdgeInsets
to 0 all around:You still get the the vertical padding for some reason.
I remember seeing an answer which I cannot find now where it suggested to set an extremely small edge inset and this should work:
You can use the button’s configuration to get more precise control over its appearance like so:
Of course you don’t need to use updateHandler, you can just access the configuration directly and just set it there
See if this solves the problem…
You can use
UIButton.Configuration
and then set itscontentInsets
to.zero