Swift 3:图像不适合圆形边框
我需要在导航栏标题的圆形边框上放置一个图像。下面的代码应该放置一个圆形边框并设置一个约束。问题是这个圆圈比我上传的图片上显示的要大。我能做什么?谢谢Swift 3:图像不适合圆形边框,swift,image,border,geometry,Swift,Image,Border,Geometry,我需要在导航栏标题的圆形边框上放置一个图像。下面的代码应该放置一个圆形边框并设置一个约束。问题是这个圆圈比我上传的图片上显示的要大。我能做什么?谢谢 let imageView = UIImageView(image: profileImageResized) imageView.translatesAutoresizingMaskIntoConstraints = false imageView.frame = CGRect(x: 0, y: 0
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])
您应该将imageView的contentMode设置为.scaleSpectFill。这样,图像将填充其框架,同时也尊重其外观
imageView.contentMode = .scaleAspectFill
那个男人,谢谢你的回答。我已经试过imageView.contentMode=.scaleAspectFill和imageView.contentMode=.scaleToFill,但都不起作用。@Enzo你能用imageView.backgroundColor=UIColor.green检查你的图像上没有透明部分吗?Larme,我确实把背景色设置为绿色了。现在圆圈的空白部分被绿色填充。@Enzo这意味着图像没有覆盖视图的部分实际上是透明的。谢谢Larme和4kman。我操纵图像以去除透明度,效果良好。将此imageView.layer.masksToBounds设置为true