Search code examples
iosuiimageviewswiftsubviewbounds

UIImageView Scale to fill does not clip subviews


I am trying to add some images to my UIImageView. Everything works fine if the png has the same size as the ImageView.

But when I insert a bigger image, it is not discarded.

This is my code:

@IBOutlet weak var PictureView: UIImageView!

func loadSomePicture() {
    var examplePicture = UIImage(named: "Block.png")
    PictureView.image = exampleProfilePicture
    PictureView.layer.cornerRadius = 50
    PictureView.clipsToBounds = true

Althoug the clipsToBounds property is set true and the View Mode is Scale to fill, the image stays in its original size.

Thanks for Your help!


Solution

  • I believe the problem is occurring because you are setting the contentMode to "Aspect Fill", instead of "Scale To Fill". Can you maybe explain a little more what you want the ImageView and Image to end up as? "Aspect Fill" will always try and keep the image's aspect ratio while filling it to the ImageView's size dimensions.