UIImage Aspect Fill和clipsToBounds

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

我想将图像设置为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
    }
}
swift uiimage cliptobounds
1个回答
2
投票

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

title.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 751), for: .vertical)
© www.soinside.com 2019 - 2024. All rights reserved.