gpt4 book ai didi

swift - UIImage Aspect Fill 和 clipsToBounds

转载 作者:行者123 更新时间:2023-11-28 11:36:35 26 4
gpt4 key购买 nike

我想将图像设置为 UIImage,但是一旦设置它,UIImageView 就会变大并覆盖其他元素。

这里提供了一个解决方案: Crop UIImage to fit a frame image

但我无法让它工作,我设置了提供的解决方案中的属性。

也许有人可以看看并帮助我:

不设置图片:

enter image description here

设置图片后: enter image description here

class Cell: UICollectionViewCell, CollectionViewCellConfigurable {

var image = UIImageView()
var dateDay = UILabel()
var dateMonth = UILabel()
var title = UILabel()

lazy private var dateContainer: UIView = {
let v = UIView()
v.sv(dateDay, dateMonth)

dateDay.heightAnchor.constraint(equalTo: self.dateMonth.heightAnchor, multiplier: 1).isActive = true
dateDay.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 5).isActive = true
dateDay.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -5).isActive = true
dateDay.topAnchor.constraint(equalTo: v.topAnchor, constant: 15).isActive = true
dateDay.bottomAnchor.constraint(equalTo: dateMonth.topAnchor, constant: -5).isActive = true

dateMonth.heightAnchor.constraint(equalTo: self.dateDay.heightAnchor, multiplier: 1).isActive = true
dateMonth.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 5).isActive = true
dateMonth.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -5).isActive = true
dateMonth.topAnchor.constraint(equalTo: dateDay.bottomAnchor, constant: 5).isActive = true
// dateMonth.bottomAnchor.constraint(greaterThanOrEqualTo: v.bottomAnchor, constant: -5).isActive = true
return v
}()

lazy private var container: UIView = {
return UIView()
}()

func configureCellAtIndexPath(item: Journaling) {
self.image.image = UIImage.baliBeach
self.image.backgroundColor = .green
self.dateDay.text = "16"
self.dateMonth.text = "May"
self.title.text = "Text visible"

}

override init(frame: CGRect) {
super.init(frame:frame)
setUpLayout()
additionalSetUp()
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func additionalSetUp() {
backgroundColor = .clear
let colorText: UIColor = .grau

dateDay.textColor = colorText
dateMonth.textColor = colorText
title.textColor = colorText
dateDay.textAlignment = .center
dateMonth.textAlignment = .center
title.textAlignment = .center
image.contentMode = .scaleAspectFill
image.clipsToBounds = true
image.autoresizesSubviews = true

}

func setUpLayout() {
sv(dateContainer, container)

container.sv(image,title)

dateContainer.widthAnchor.constraint(equalToConstant: 45).isActive = true
// dateContainer.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.15).isActive = true
dateContainer.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
dateContainer.trailingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
dateContainer.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
dateContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

// container.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
container.leadingAnchor.constraint(equalTo: dateContainer.trailingAnchor, constant: 0).isActive = true
container.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

image.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 5).isActive = true
image.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -5).isActive = true
image.topAnchor.constraint(equalTo: self.topAnchor, constant: 15).isActive = true
image.bottomAnchor.constraint(equalTo: title.topAnchor, constant: -5).isActive = true

title.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
title.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: 0).isActive = true
title.topAnchor.constraint(equalTo: image.bottomAnchor, constant: 5).isActive = true
title.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: 0).isActive = true
}
}

最佳答案

标题标签的垂直压缩优先级需要更高,因为imageView等于它= 750,所以它占用空间

title.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 751), for: .vertical)

关于swift - UIImage Aspect Fill 和 clipsToBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55087538/

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