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

标签 ios swift xcode

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

相关文章:

ios - 如何让多个音频流在 iOS 中重叠播放?

ios - 如何在 Swift 中缓存 double 数组

ios - 模拟器无法升级 xcode 10?

ios - 快速更新循环

ios - 如何将点击监听器添加到选定的 mkannotationview?

iphone - 内存管理如何修复泄漏

ios - ReactiveCocoa - concat 展平策略未按预期工作

ios - swift.h 找不到 Swift 类

ios - 在 Finder 中更改文件后,Xcode 无法自行找到该文件

objective-c - 为什么 NSInteger 变量在用作格式参数时必须转换为 long?