UIButton/配置 titleAlignment = .leading 和 imagePlacement = .trailing 不受尊重

问题描述 投票:0回答:1
        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)

Centered for some reason

如何将标题固定在按钮的前缘,将图像固定在后缘?谢谢!

将对齐方式更改为前导会将标题和图像移动到前缘。

enter image description here

uikit uibutton
1个回答
0
投票

关键是将按钮的

.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),
])

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.