我正在使用ImageView和CollectionView创建一个图像库。一切正常,但肖像模式的图片超出了限制。为什么?

    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

ios - 当UIImageView的内容模式为.scaleAspectFill时,为什么它在肖像图片中会超出其约束?-LMLPHP

最佳答案

你需要设置

myPicture.clipsToBounds = true

10-07 14:54