1

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

Tigran
  • 235
  • 5
  • 14

1 Answers1

2

You need to set

myPicture.clipsToBounds = true
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87