在UIImageView周围填充。斯威夫特3

时间:2017-05-28 19:04:05

标签: swift uiimageview

我有一个UIImageView,我不希望图像触及'视图的边缘,而不是有一些填充'周围。但是,我尝试了以下内容,由于某些原因它没有改变:

 @IBOutlet weak var pictureOutletOne: UIImageView!

//set the image
pictureOutletOne.image = UIImage(named: itemOne)

//set the padding
pictureOutletOne.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);

我也尝试过:

pictureOutletOne.translatesAutoresizingMaskIntoConstraints = false
pictureOutletOne.layoutMargins = UIEdgeInsets(top: 10, left: 100, bottom: 10, right: 0)

我已经阅读了很多关于此的内容,但这些是我找到的解决方案,但它们并不起作用。使用swift 3

非常感谢。

4 个答案:

答案 0 :(得分:3)

您可以将imageView插入视图并将约束设置为边,保证接近:)

答案 1 :(得分:1)

Swift 4.2和5

let imageView = UIImageView()
imageView.image = UIImage(named: 
"image")?.withAlignmentRectInsets(UIEdgeInsets(top: -5, left: -5, bottom: -5, 
right: -5))

插入值应为负值

答案 2 :(得分:0)

在新类中覆盖alignmentRectInsets属性:

    class PaddedImageView: UIImageView {
        override var alignmentRectInsets: UIEdgeInsets {
            return UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
        }
    }

答案 3 :(得分:0)

let padding: CGFloat = 10    
myImageView.contentMode = .scaleAspectFill
myImageView.image = UIImage(named: "myImage.png").resizableImage(withCapInsets: UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), resizingMode: .stretch)