按钮图像上的中心标题

uya*_*arc 1 button ios swift

我想以编程方式更改按钮图像和标题。当在故事板上设计按钮图像和标题(普通)时,它居中没有问题。但是当\xc4\xb1使用时;

\n
  startButton.setImage(UIImage(named: "StartButton"), for: .normal)\n  startButton.setAttributedTitle(NSAttributedString(string: "START"), for: .normal)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

Muh*_*waz 7

这可能对你有帮助。使用 setBackgroundImage 而不是 setImage

startButton.setBackgroundImage(UIImage(named: "StartButton"), for: .normal)
Run Code Online (Sandbox Code Playgroud)

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