i am trying create triangle like custom view but it is not showing. i am not getting what is the wrong
i am referring this Make UILabel at 45 degree with a cross background colour swift
class ViewController: UIViewController {
@IBOutlet weak var testView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let triangleView = TriangleView()
triangleView.draw(CGRect(x: 0, y: 0, width: 50, height: 50))
testView.addSubview(triangleView)
}
}
class TriangleView: UIView {
override func draw(_ rect: CGRect) {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: rect.maxX, y: 0))
path.addLine(to: CGPoint(x: 0, y: rect.maxY))
path.close()
UIColor.red.withAlphaComponent(1).setFill()
path.fill()
}
}
want to create custom view
You don't need to call draw() method explicitly. Just set the frame for the triangle view.
override func viewDidLoad() {
super.viewDidLoad()
let customView = TriangleView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
testView.addSubview(customView)
}
Hope it helps!!!