I would like to create a square UIView with a 50px diameter half-circle cutting it at the bottom, like this:
I tried this code:
class CustomView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
let radius = 50.0
let path = UIBezierPath(
arcCenter: CGPoint(
x: bounds.midX,
y: bounds.maxY
),
radius: radius,
startAngle: .pi,
endAngle: 0,
clockwise: true
)
path.addLine(
to: CGPoint(
x: bounds.minX,
y: bounds.maxY
)
)
path.close()
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
}
But instead, I am getting this:
How can I get this to work?


Here's a solution that involves overriding
drawand using aUIBezierPathto draw the shape.The following can be used in an iOS Swift Playground:
This will work for any view size with properties to set the radius of the semi-circle and the fill color.
Here's an update to your code that fixes your use of a mask: