我在堆栈视图中有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
Run Code Online (Sandbox Code Playgroud)
您可以添加高度和宽度的约束:
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])
Run Code Online (Sandbox Code Playgroud)
只要确保按比例设置stackview的分布即可:
releaseInfoStack.distribution = .fillProportionally
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6253 次 |
| 最近记录: |