这就是我使用此代码所获得的
private func setupBorders(){
let path = UIBezierPath(roundedRect: mainTableView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20, height: 20))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
mainTableView.layer.mask = maskLayer
mainTableView.layer.borderColor = UIColor.darkGray.cgColor
mainTableView.layer.borderWidth = 1.0
}
MainTableView是一个包含记事本表和表头的uiview。如果我可以让它为任何UIView工作,那么它将适用于这个。非常感谢任何可以提供帮助的人!
编辑:如果不清楚,问题是边框在圆角处消失。
答案 0 :(得分:0)
遮罩层不足以满足您的要求,因为图层边框不会与图层蒙版相关。相反,你应该创建一个用于绘制背景和边框的视图,它也应该沿着边框剪切其内容。
答案 1 :(得分:0)
在storyboard中将UIView拖动到ViewController,根据需要设置约束,将其链接到NewView并尝试此操作,
import UIKit
import QuartzCore
class ViewController: UIViewController{
@IBOutlet var NewView :UIView!
override func viewDidLoad() {
super.viewDidLoad()
let screenSize: CGRect = UIScreen.mainScreen().bounds
let HeightFloat :CGFloat = screenSize.height - 60
let WidthFloat :CGFloat = screenSize.width - 50
let NewRect :CGRect = CGRectMake(10, 20, WidthFloat, HeightFloat)
let maskPath = UIBezierPath(roundedRect: NewRect,
byRoundingCorners: [.TopLeft, .TopRight],
cornerRadii: CGSize(width: 10, height: 10))
let shape = CAShapeLayer()
shape.path = maskPath.CGPath
shape.fillColor = UIColor.clearColor().CGColor
shape.strokeColor = UIColor.darkGrayColor().CGColor
shape.lineWidth = 2
shape.lineJoin = kCALineJoinRound
NewView.layer.mask = shape
}
}