如何使用.scaleAspectFit和.top在UIImageView中正确对齐我的图像?

问题描述 投票:1回答:1

我似乎无法找出一种方法来将我的图像对准此UIImageView的顶部,同时还要使用.scaleAspectFit。我以蓝色突出显示了ImageView,以便您可以看到发生了什么。如您所见,图像垂直居中,但我要求将其固定在顶部。有没有一种方法可以将其设置为.scaleAspectFit中的UIStackView和.top?

class SwipedPageCell: UICollectionViewCell  {

override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews(distribution: .fillProportionally, alignment: .fill, spacing: 0)

    backgroundColor = UIColor.backgroundGrey()

}

let imageScreenshot: UIImageView = {
   let iv = UIImageView()
    iv.image = UIImage(named: "tv-screenshot")
    iv.contentMode = .scaleAspectFit
    iv.clipsToBounds = true
    iv.translatesAutoresizingMaskIntoConstraints = false

    iv.backgroundColor = .blue
    return iv
}()

let logoText: UILabel = {
    let label = UILabel()
    label.attributedText = NSAttributedString(string: "whatsong", attributes: [
        NSAttributedString.Key.kern: -1.0
        ])
    label.font = UIFont(name: "FatFrank", size: 40)
    label.textColor = UIColor(red: 41/255, green: 45/255, blue: 51/255, alpha: 1)
    label.textAlignment = .center
    return label
}()

let subheading: UILabel = {
    let label = UILabel()
    label.attributedText = NSAttributedString(string: "Discover music from the latest movies and television shows", attributes: [
        NSAttributedString.Key.kern: -0.6
        ])
    label.font = UIFont(name: "Montserrat-SemiBold", size: 16)
    label.textColor = UIColor(red: 41/255, green: 45/255, blue: 51/255, alpha: 1)
    label.numberOfLines = 0
    label.textAlignment = .center
    return label
}()

func setupViews(distribution: UIStackView.Distribution, alignment: UIStackView.Alignment, spacing: CGFloat)   {

    let verticalStackView = VerticalStackView(arrangedSubviews: [imageScreenshot, logoText, subheading])
    verticalStackView.spacing = spacing
    verticalStackView.distribution = distribution
    verticalStackView.alignment = alignment

    addSubview(verticalStackView)

    verticalStackView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 0, left: 20, bottom: 10, right: 20))

}
}
ios swift uiimageview uistackview
1个回答
0
投票

我也很好奇是否有人对此问题有建议。

© www.soinside.com 2019 - 2024. All rights reserved.