简体   繁体   中英

What causes horizontal padding around title of UIButton?

I programmatically created two UIButton s and constrained them to 2 points apart (horizontally). I see extra space, so I clicked the "Debug View Hierachy" button in Xcode, and here is what I see:

在此处输入图片说明

What is causing the extra space around the "abc" title? I looked at a few suspect properties of the button and they are all zero. ( contentEdgeInsets , titleEdgeInsets )

I haven't done anything to the buttons other than:

let b = UIButton(type: .system)
b.translatesAutoresizingMaskIntoConstraints = false
b.setTitle("...", for: .normal)

The constraints look like:

b1.centerXAnchor.constraint(equalTo: outer.centerXAnchor)
b2.leftAnchor.constraint(equalTo: b1.rightAnchor, constant: 2)
b2.firstBaselineAnchor.constraint(equalTo: b1.firstBaselineAnchor, constant: 0)  

UIButton has an intrinsic minimum width of 30-pts.

So, if the title text is shorter than 30-pts, the button's title label will be centered horizontally in the button's 30-pt frame.

You can explicitly set the width to less than 30, but then you'll have to take some other steps to get your button to auto-size with a longer title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM