var configuration = UIButton.Configuration.bordered()
configuration.image = ImageProvider.icon.history
configuration.imagePlacement = .trailing
historyButton.configuration = configuration
configuration.titleAlignment = .leading
historyButton.setTitle(String(localized: "History"), for: .normal)
let image = ImageProvider.icon.history
historyButton.setImage(image, for: .normal)
如何将标题固定在按钮的前缘,将图像固定在后缘?谢谢!
将对齐方式更改为前导会将标题和图像移动到前缘。
关键是将按钮的
.contentHorizontalAlignment
设置为.fill
...
var configuration = UIButton.Configuration.bordered()
configuration.image = UIImage(systemName: "gobackward.15")
configuration.title = "History"
configuration.imagePlacement = .trailing
let btn = UIButton(configuration: configuration)
btn.contentHorizontalAlignment = .fill
btn.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(btn)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
btn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0),
btn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -60.0),
])
结果: