无法设置 UIImageView 的大小

Can't set size for UIImageView

我有一个 UIImageView,我想给它一个固定的大小,但它不起作用,UIImageView 的大小不会改变。 这是我的 UIImageView:

private let profileImg: UIImageView = {
    let img = UIImageView(image: UIImage(named: "profileplaceholder"))
    img.contentMode = .scaleAspectFill
    img.layer.borderWidth = 1.0
    img.layer.borderColor = UIColor.appPurple.cgColor
    img.layer.cornerRadius = img.frame.width / 2
    img.clipsToBounds = true
    img.frame.size.width = 80
    img.frame.size.height = 80
    return img
}()

我将图像添加到 UIStackView 中:

private lazy var mStack: UIStackView = {
    let stack = UIStackView(arrangedSubviews: [self.profileImg, self.nickname, self.unfollowBtn])
    stack.translatesAutoresizingMaskIntoConstraints = false
    stack.distribution = .fill
    stack.axis = .horizontal
    stack.spacing = 1.0

    return stack
}()

StackView 将调整其元素的大小。你应该给它固定大小的约束:

private let profileImg: UIImageView = {
...
    img.translatesAutoresizingMaskIntoConstraints = false
    img.heightAnchor.constraint(equalToConstant: 80).isActive = true
    img.widthAnchor.constraint(equalToConstant: 80).isActive = true
...
}()