iosswiftuiimageviewautolayoutcontentmode

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


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


Solution

  • You need to set

    myPicture.clipsToBounds = true