I've been trying to create a triangle shaped button. To do that, initially I've tried to create a basic triangle shape with using BezierPath.
In order to create a triangle shape; I've created a UIView Class. Here is the code:
class BezierView: UIView {
override func draw(_ rect: CGRect) {
let triangle = UIBezierPath()
triangle.move(to: CGPoint(x: 200, y: 200))
triangle.addLine(to: CGPoint(x: 160, y: 240))
triangle.addLine(to: CGPoint(x: 250, y: 240))
triangle.close()
}
After creating that class of Bezierview, I've tried to call this custom class in my main ViewController.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let bezierView = BezierView(frame: CGRect(x: 100, y: 100, width: 10, height: 10)) // Here I'm trying to determine the location and size of the triangle
bezierView.backgroundColor = .black
self.view.addSubview(bezierView)
}
}
What this code does is only bringing a black rectangle Shape to the screen. While I'm trying to draw a basic triangle, my code block is not working.
Although after creating that kind of a triangle shape, I want to use it as the frame of a UIButton. So is that something possible to do? Can I set the frame of a button to that custom made triangle? From what I know; frame is a CGRect and can not be converted to UIBezierPath. What can I do about these 2 problems. (First creating a triangle then shaping a button frame as that custom triangle shape)
Thank you for your help. Hopefully everything was clearly explained.
Cheers.