【问题标题】:Center title on the button image按钮图像的中心标题
【发布时间】:2021-12-28 10:34:24
【问题描述】:

我想以编程方式更改按钮图像和标题。在情节提要上设计按钮图像和标题(纯文本)时,它居中没有问题。但是当我使用时;

  startButton.setImage(UIImage(named: "StartButton"), for: .normal)
  startButton.setAttributedTitle(NSAttributedString(string: "START"), for: .normal)

以编程方式更改按钮图像和标题。按钮标题未以图像为中心。它看起来在图像的左侧。

【问题讨论】:

    标签: ios swift button


    【解决方案1】:

    这可能会对您有所帮助。 使用 setBackgroundImage 代替 setImage

    startButton.setBackgroundImage(UIImage(named: "StartButton"), for: .normal)
    

    希望这个解决方案能解决您的问题?

    【讨论】:

    • 感谢@Muhammad Nawaz。我也使用它,但 setTitle 不是 setAttributedTitle。在你回答后,我再次检查。它解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多