簡體   English   中英

如何從UIButton中刪除左邊距?

[英]How to remove left padding from UIButton?

如何從UIButton刪除左邊padding

我已經使用以下代碼創建了按鈕:

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

您需要使用一些負左值調整imageEdgeInsets和titleEdgeInsets。 然后它會轉移到左邊。 我測試了它的工作原理。 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)

如果它不起作用,請告訴我。

此行可以解決您的問題

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

或者也許你可以在這種情況下使用負值

還有這種方式:

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

試試不同的價值!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM