Why UIImageView when its content mode is .scaleAspectFill in portrait pictures goes over its constraints?

334 views Asked by At

I'm creating an image gallery using ImageView and CollectionView. Everything is working fine but portrait mode pictures go over constraints. Why is that?

    myPicture = UIImageView(image: myArray[0])
    view.addSubview(myPicture)
    myPicture.translatesAutoresizingMaskIntoConstraints = false
    myPicture.contentMode = .scaleAspectFill
    myPicture.topAnchor.constraint(equalTo: tabBar.bottomAnchor, constant: view.bounds.height / 80).isActive = true
    myPicture.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    myPicture.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
    myPicture.bottomAnchor.constraint(equalTo: collectionView.topAnchor, constant:  0).isActive = true

enter image description here

1

There are 1 answers

0
Shehata Gamal On BEST ANSWER

You need to set

myPicture.clipsToBounds = true