I have a custom UIView
subclass, that I want to add as a subview on my UIViewController
. The problem is, that the view doesn't show up, when I run the app, even though everything is set correctly (in viewDidLoad
the view has a correct frame and is not hidden
) and it shows up in Interface Builder (picture). On the device I only get a red screen, no triangle in the middle.
Here's the view subclass:
@IBDesignable
class TriangleView: UIView {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let path = UIBezierPath()
let width = rect.size.width
let height = rect.size.height
let startingPoint = CGPoint(x: center.x, y: center.y - height / 2)
path.moveToPoint(startingPoint)
path.addLineToPoint(CGPoint(x: center.x + width / 2, y: center.y - height / 2))
path.addLineToPoint(CGPoint(x: center.x, y: center.y + height / 2))
path.addLineToPoint(CGPoint(x: center.x - width / 2, y: center.y - height / 2))
path.closePath()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = rect
shapeLayer.position = center
shapeLayer.path = path.CGPath
shapeLayer.fillColor = UIColor.whiteColor().CGColor
layer.mask = shapeLayer
layer.backgroundColor = UIColor.redColor().CGColor
}
}
I don't have any other code to show, I just add the view to the ViewController
and set its constraints and class to TriangleView
.