John Doah
John Doah

Reputation: 1999

Can't set size for UIImageView

I have a UIImageView, I'm trying to give it a fixed size, but it won't work, the size of the UIImageView just won't change. this is my 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
}()

I added the image to a UIStackView like that:

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
}()

Upvotes: 0

Views: 32

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119686

StackView will resize it's elements. You should give it constraint for fixed size:

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

Upvotes: 3

Related Questions