我在堆栈 View 中有 2 个项目,一个是图像,另一个是标签。我希望图像根据其帧大小而不是堆栈大小调整大小我该如何更改。
//stack code
lazy var releaseInfoStack:UIStackView = {
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.spacing = 4
v.alignment = .center
v.axis = .horizontal
return v
}()
//my image
lazy var smallRealeaseImageIcon:UIImageView = {
let v = UIImageView(frame:CGRect(x: 0, y: 0, width: 17, height: 17))
v.contentMode = .scaleAspectFit
v.image = UIImage(named: "link")
return v
}()
//this is the current image below but i want the link icon to be smaller
最佳答案
您可以为高度和宽度添加约束:
let imageViewWidthConstraint = NSLayoutConstraint(item: smallRealeaseImageIcon, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 20)
let imageViewHeightConstraint = NSLayoutConstraint(item: smallRealeaseImageIcon, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 20)
smallRealeaseImageIcon.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint])
只需确保将 stackview 的分布设置为按比例:
releaseInfoStack.distribution = .fillProportionally
关于ios - Swift 如何在堆栈 View 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42612270/