gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:55:17 25 4
gpt4 key购买 nike

我似乎找不到一种将图像对准此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 - 如何使用.scaleAspectFit和.top在UIImageView中正确对齐图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56733646/

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