【问题标题】:Swift3 round two corners of UIViewSwift3 圆了 UIView 的两个角
【发布时间】:2017-03-25 17:29:16
【问题描述】:

我知道这个问题已经问了很多次了,他们的解决方案也可用,但对我没有用。 我正在使用 Swift3,我想为此目的对 UIView 的任意两侧进行四舍五入我找到了以下解决方案

Create a rectangle with just two rounded corners in swift?

以下是我上述解决方案的代码sn-p

extension UIView {

    /**
     Rounds the given set of corners to the specified radius

     - parameter corners: Corners to round
     - parameter radius:  Radius to round to
     */
    func round(corners: UIRectCorner, radius: CGFloat) -> Void {
         layer.addSublayer(_round(corners: corners, radius: radius))
    }

    /**
     Rounds the given set of corners to the specified radius with a border

     - parameter corners:     Corners to round
     - parameter radius:      Radius to round to
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        let mask = _round(corners: corners, radius: radius)
        addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
    }

    /**
     Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border

     - parameter diameter:    The view's diameter
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        layer.masksToBounds = true
        layer.cornerRadius = diameter / 2
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor;
    }

}

private extension UIView {

    func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
        return mask
    }

    func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
        let borderLayer = CAShapeLayer()
        borderLayer.path = mask.path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = bounds
        layer.addSublayer(borderLayer)
    }

}

这是我得到的结果。我不知道我做错了什么。 谁能解决这个问题?

【问题讨论】:

  • 给我们看一些代码

标签: ios swift3 xcode8


【解决方案1】:

代码在 Swift 3 中经过测试和工作。

使用下面的extension 创建roundCorners

extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
    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
}
}

用法:viewDidLoad

yourViewName.roundCorners(corners: [.topRight, .bottomLeft], radius: 10)

输出:

【讨论】:

  • 这是最好的短片和甜美的 :)
  • 在 viewDidLoad() 中调用 roundCorners() 在视图布局之前应用掩码,并且不考虑任何未来的布局更改。如果你打算从 ViewController 调用它,你应该在 viewDidLayoutSubviews() 中调用它。另一种选择是创建一个 UIView 子类,其中包含角和半径的变量,以更新其 layoutSubviews() 中的掩码
【解决方案2】:

首先,您需要为您的文本字段或按钮制作 IBOutlet 需要圆角

  1. 转到 ViewDidLoad() 方法
  2. 写IBOutLet的名字
  3. 示例代码 mybutton.layer.cornerRadius=10
  4. 这是您问题的解决方案

【讨论】:

  • 他专门问你如何只圆两个角,你的回答设置了所有角的圆角半径。
猜你喜欢
  • 2011-06-18
  • 2010-12-03
  • 2011-05-13
  • 2013-05-15
  • 2011-01-24
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
相关资源
最近更新 更多