isaacthedev
isaacthedev

Reputation: 201

UIImageView is spilling outside collection view cells

enter image description hereenter image description hereUIImage is expanding outside the collection view cell. The issue started when I added shadows to the cell.

The UI is done in code, when I added shadows to the cell specifically when I set maskToBounds on the cell to be false, the image expands outside the bounds of the cells.

    static let identifier = "kParameterCollectionViewCell"
    let bgView = UIView()
    let imageView = UIImageView()
    let nameLabel = UILabel()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.clipsToBounds = true
        self.autoresizesSubviews = true
        self.backgroundColor = UIColor.clear
        self.layer.cornerRadius = 20.0
        // Add Shadows
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowRadius = 1.0
        self.layer.shadowOpacity = 1.0
        self.layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
        self.layer.masksToBounds = false

        bgView.frame = self.bounds
        bgView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        bgView.backgroundColor = .clear

        imageView.translatesAutoresizingMaskIntoConstraints = false
        //imageView.backgroundColor = UIColor.clear
        imageView.layer.cornerRadius = 2.0
        imageView.layer.borderColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
        imageView.backgroundColor = UIColor.clear
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.layer.masksToBounds = true
        self.addSubview(imageView)

I expect the image view to be on the far right of the cell.

I will try to post two photos, the first will be what is happening and the second will be what I expect.

Upvotes: 0

Views: 409

Answers (1)

kannan
kannan

Reputation: 364

        imageView.contentMode = .scaleAspectFit

Try with aspect fit.

Upvotes: 2

Related Questions