gpt4 book ai didi

ios - Swift 如何在堆栈 View 中调整图像大小

转载 作者:搜寻专家 更新时间:2023-10-31 22:28:34 25 4
gpt4 key购买 nike

我在堆栈 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

Current Image

最佳答案

您可以为高度和宽度添加约束:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com