Ios 如何设置imageView的角半径?

Ios 如何设置imageView的角半径?,ios,swift,uiimageview,cornerradius,Ios,Swift,Uiimageview,Cornerradius,在Objective-C中,这一行 self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f; 做它的工作,我试着用斯威夫特类比 self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0 而且它没有改变任何东西,角落和以前一样。此外,Xcode没有显示任何语法错误。Sw

在Objective-C中,这一行

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;
做它的工作,我试着用斯威夫特类比

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0

而且它没有改变任何东西,角落和以前一样。此外,Xcode没有显示任何语法错误。Swift是否支持实现这一目标的其他方式?我在这里检查了一些其他线程,通常都是以上面显示的方式在Swift中完成的

图层从剪辑区域中绘制,您需要将其设置为“遮罩到边界”:

self.mainImageView.layer.masksToBounds = true
从:

默认情况下,角半径不应用于中的图像 层的内容属性;它仅适用于背景色和颜色 层的边界。但是,将masksToBounds属性设置为 true会将内容剪裁到圆角

试试这个

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
self.mainImageView.clipsToBounds = true

在swift 3中,“CGRectGetWidth”已替换为属性“CGRect.width”

    view.layer.cornerRadius = view.frame.width/4.0
    view.clipsToBounds = true

swift3.0和Xcode8中有一个微小的区别

无论何时要将角半径应用于UIView,请确保调用 调用
cornerRadius
之前,请先使用yourUIView.layoutifNeed()

否则,它将返回UIView高度和宽度的默认值(1000.0),这可能会使视图消失

在设置任何图层特性之前,始终确保应用了更改UIView大小的所有效果(界面生成器约束等)

UIView类实现示例

class BadgeView: UIView {

  override func awakeFromNib() {

    self.layoutIfNeeded()
    layer.cornerRadius = self.frame.height / 2.0
    layer.masksToBounds = true

   }
 }

Swift 3、Xcode 8、iOS 10

DispatchQueue.main.async {
  self.mainImageView.layer.cornerRadius = self.mainImageView.bounds.size.width / 2.0
  self.mainImageView.clipsToBounds = true
}

最简单的方法是创建UIImageView子类(我已经尝试过了,它在iPhone7和XCode 8上运行得非常好):

然后还可以设置边框:

imageView.layer.borderWidth = 2.0

imageView.layer.borderColor = UIColor.blackColor().CGColor

在swift中标记有
@IBInspectable
(或在Objective-C中标记为IBInspectable),它们可以在Interface Builder的属性检查器面板中轻松编辑。
您可以在属性检查器中直接设置边框宽度拐角半径边框颜色

extension UIView {

  @IBInspectable var cornerRadius: CGFloat {

   get{
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
  }

  @IBInspectable var borderWidth: CGFloat {
    get {
        return layer.borderWidth
    }
    set {
        layer.borderWidth = newValue
    }
  }

  @IBInspectable var borderColor: UIColor? {
    get {
        return UIColor(cgColor: layer.borderColor!)
    }
    set {
        layer.borderColor = borderColor?.cgColor
    }
  }
}

您可以定义任何视图的边界半径,提供“用户定义的运行时属性”,提供字符串类型的键路径“layer.cornerRadius”,然后定义所需半径的值;) 请参阅下面的附图:


我创建了一个
UIView
扩展,它允许对特定的角进行圆角:

import UIKit

enum RoundType {
    case top
    case none
    case bottom
    case both
}

extension UIView {

    func round(with type: RoundType, radius: CGFloat = 3.0) {
        var corners: UIRectCorner

        switch type {
        case .top:
            corners = [.topLeft, .topRight]
        case .none:
            corners = []
        case .bottom:
            corners = [.bottomLeft, .bottomRight]
        case .both:
            corners = [.allCorners]
        }

        DispatchQueue.main.async {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }

}

根据@DCDC的回答

self.mainImageView.layer.cornerRadius=(self.mainImageView.frame)/2.0“CGRect不能转换为double”self.mainImageView.layer.cornerRadius=(self.mainImageView.frame.size.height)/2.0也不能工作,我错过了那一行,现在工作非常好,谢谢@DarthMike!这个答案对于从url获取图像并设置为imageviewmy Guuuuyyyyyy nice后的圆角是正确的!在更新到Swift 3.0 Border Color不起作用后,我还需要masksToBounds才能让它工作。它总是返回黑色。@JoeSene layer.borderColor=borderColor?.cgColor错误,只需像这样更新它:layer.borderColor=newValue?.cgcolor这是一个干净而简单的解决方案,从主线程调用帮助了我,因为我在
tableView委托方法中舍入了
containerView
——它是我的tableView单元格的子视图。没有从主线程调用它,框架是错误的
import UIKit

enum RoundType {
    case top
    case none
    case bottom
    case both
}

extension UIView {

    func round(with type: RoundType, radius: CGFloat = 3.0) {
        var corners: UIRectCorner

        switch type {
        case .top:
            corners = [.topLeft, .topRight]
        case .none:
            corners = []
        case .bottom:
            corners = [.bottomLeft, .bottomRight]
        case .both:
            corners = [.allCorners]
        }

        DispatchQueue.main.async {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }

}
import UIKit

class BorderImage: UIImageView {

    override func awakeFromNib() {
        self.layoutIfNeeded()
        layer.cornerRadius = self.frame.height / 10.0
        layer.masksToBounds = true
    }
}