Reputation:
I want to create custom view but it is not loading.
I am trying Parallelogram view using UIBezierPath
class ViewController: UIViewController {
@IBOutlet weak var testView: UIView!
override func viewWillAppear(_ animated: Bool) {
let customeView = CustomeView()
customeView.draw(CGRect(x: 0, y: 0, width: 50, height: 50))
testView.addSubview(customeView)
}
}
class CustomeView: UIView {
override func draw(_ rect: CGRect) {
let path = UIBezierPath()
path.move(to: CGPoint(x: bounds.minX , y: bounds.minY))
path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
path.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
path.addLine(to: CGPoint(x: bounds.minX, y: bounds.minY))
path.close()
UIColor.red.setFill()
path.fill()
}
}
I am expecting to create custom view.
Upvotes: 0
Views: 219
Reputation: 318924
Never call a view's draw
method yourself. Remove that line. The proper solution is to give the view a proper frame. And you should set up the view in viewDidLoad
.
override func viewDidLoad() {
super.viewDidLoad()
let customeView = CustomeView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
testView.addSubview(customeView)
}
Upvotes: 3