【问题标题】:How to remove left padding from UIButton?如何从 UIButton 中删除左填充?
【发布时间】:2017-04-16 09:47:08
【问题描述】:

如何从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

【问题讨论】:

  • 简单 button.setTitle("+ Text") 有效吗? (又名,不嵌入真实图像)

标签: ios swift uibutton imageedgeinsets titleedgeinsets


【解决方案1】:

此行可以解决您的问题

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

或者在这种情况下你可以使用负值

还有这种方式:

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

尝试不同的值!

【讨论】:

  • 如果还是不行,说明你的图片太大了,应该调整大小
【解决方案2】:

您需要调整 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)

如果它不起作用,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2021-06-09
    • 2016-05-30
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多