简体   繁体   English

Swift如何在stackview中调整图像大小

[英]Swift How to resize an image inside a stackview

I have 2 items in a stack view one is an image and the other is a label. 我在堆栈视图中有2个项目,一个是图像,另一个是标签。 I want the image to resize according to its frame size not the size of stack how can i change this. 我希望图像根据其帧大小调整大小而不是堆栈的大小我该怎么改变它。

//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

目前的形象

You can add constraints for height and width: 您可以添加高度和宽度的约束:

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

Just make sure to set the stackview's distribution to proportionally: 只需确保按比例设置stackview的分布:

releaseInfoStack.distribution = .fillProportionally

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM