为Swift中的Button内部图像设置contentMode

问题描述 投票:0回答:1

我正在尝试将setImage放入UIButton,但尽管有“自由空间”,但按钮内的图像显得较小,我也设置了contentMode

按钮:

let noteButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "pencil"), for: .normal)
    v.tintColor = UIColor.white
    v.imageView?.contentMode = .scaleAspectFit
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

约束:

noteButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    noteButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
    noteButton.centerYAnchor.constraint(equalTo: itemView.centerYAnchor).isActive = true
    noteButton.leadingAnchor.constraint(equalTo: linkButton.leadingAnchor, constant: 50).isActive = true

结果:

enter image description here

ios uibutton uiimageview contentmode
1个回答
0
投票

我已解决此问题。我只需要将这些添加到行中即可:

v.contentHorizontalAlignment = .fill
v.contentVerticalAlignment = .fill
© www.soinside.com 2019 - 2024. All rights reserved.