Swift 3:图像不适合圆边框

时间:2017-10-11 14:32:22

标签: swift image border geometry

我需要在导航栏标题的圆形边框上放置图像。下面的代码应该放置一个圆边框并设置一个约束。问题是圆圈大于图像,如我上传的图像所示。我能做什么?谢谢。

        let imageView = UIImageView(image: profileImageResized)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        imageView.layer.cornerRadius = imageView.frame.size.width/2
        imageView.layer.borderWidth = 1
        imageView.layer.borderColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 100.0).cgColor
        imageView.clipsToBounds = true

        view.addSubview(imageView)

        let verticalSpace = NSLayoutConstraint(item: imageView,
                                           attribute: .top,
                                           relatedBy: .equal,
                                           toItem: self.topLayoutGuide,
                                           attribute: .bottom,
                                           multiplier: 1, constant: 0)
        view.addConstraints([verticalSpace])  

Image with imageView.clipsToBounds = true

Image without imageView.clipsToBounds = true

1 个答案:

答案 0 :(得分:1)

您应该将Promise.all的contentMode设置为imageView。这样,图像将填充其框架,同时也尊重其方面。

.scaleAspectFill