我尝试将一些项目更新为Swift 2.0。我有一个View,左上角有一个圆角。 Swift< Swift< 1.2,但现在,再也没有圆角了。
没有警告,没有错误,没有圆角。
这是它在Swift< 1.2。
let maskPath = UIBezierPath(roundedRect: contentView.bounds,byRoundingCorners: .TopLeft, cornerRadii: CGSize(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer(layer: maskPath)
maskLayer.frame = contentView.bounds
maskLayer.path = maskPath.CGPath
contentView.layer.mask = maskLayer
任何人都知道这里有什么不对吗?我没有在文档中找到任何更改。
答案 0 :(得分:5)
答案 1 :(得分:2)
Swift 4.0-5.0
您可以使用我创建的简单类来创建UIView并直接从Storyboard中添加圆角
您可以找到课程here
import Foundation
import UIKit
@IBDesignable class SwiftRoundView: UIView {
@IBInspectable fileprivate var borderColor: UIColor = .white { didSet { self.layer.borderColor = self.borderColor.cgColor } }
@IBInspectable fileprivate var borderWidth: CGFloat = 0.00 { didSet { self.layer.borderWidth = self.borderWidth } }
@IBInspectable fileprivate var cornerRadius: CGFloat = 0.00 { didSet { self.layer.cornerRadius = self.cornerRadius } }
init(x: CGFloat = 0.0, y: CGFloat = 0.0, width: CGFloat, height: CGFloat, cornerRadius: CGFloat = 0.0, borderWidth: CGFloat = 0.0, borderColor: UIColor = .white) {
self.cornerRadius = cornerRadius
self.borderWidth = borderWidth
self.borderColor = borderColor
super.init(frame: CGRect(x: x, y: y, width: width, height: height))
setupView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
fileprivate func setupView() {
self.layer.cornerRadius = cornerRadius
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.cgColor
self.clipsToBounds = true
}
}