简体   繁体   English

如何从UIButton中删除左边距?

[英]How to remove left padding from UIButton?

How to remove left padding from UIButton ? 如何从UIButton删除左边padding

I have been created button with this code: 我已经使用以下代码创建了按钮:

let button = UIButton(...)
button.setImage(UIImage(named: "plus")?.withRenderingMode(.alwaysTemplate), for: .normal)
button.setTitle("Text", for: .normal)
button.layer.cornerRadius = 8
button.layer.backgroundColor = UIColor.red.cgColor

的UIButton

You need to adjust the imageEdgeInsets and titleEdgeInsets with some negative left value. 您需要使用一些负左值调整imageEdgeInsets和titleEdgeInsets。 Then it will shift to left. 然后它会转移到左边。 I tested it's working. 我测试了它的工作原理。 100 is temp value. 100是临时值。

button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)

Let me know if it is not working. 如果它不起作用,请告诉我。

This line could fix your issue 此行可以解决您的问题

button.titleEdgeInsets.left = 0 // add left padding.

Or maybe you could use negative value in this case 或者也许你可以在这种情况下使用负值

There is also this way : 还有这种方式:

button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

Just try different values ! 试试不同的价值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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