Lines not appereing when calling subclass

My code below should draw a x and y line just a graph plaine but no line are appearing. I just dont know why they are not appearing.

import UIKit

class ViewController: UIViewController{
var box = Line()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(box)
box.drawLine()

}

}

class Line:UIView {

var line =  UIBezierPath()
var line2 =  UIBezierPath()

func drawLine() {
    line.move(to: CGPoint(x: 0, y: bounds.height / 2))
    line.addLine(to: CGPoint(x: (bounds.width) , y: bounds.height / 2))
    UIColor.black.setStroke()
    line.lineWidth = 0.1
    line.stroke()
    
    line2.move(to: CGPoint(x: bounds.width / 2, y:0 ))
    line2.addLine(to: CGPoint(x:  (bounds.width / 2) , y: (bounds.height)  ))
    
    
    
    UIColor.black.setStroke()
    line2.lineWidth = 0.1
    line2.stroke()
    
    
    
}

override func draw(_ rect: CGRect) {
    drawLine()
}

}

check the frame of the line in the view debugger / set the frame of the line

box.frame = CGRect(x: 150, y: 200, width: 200, height: 20)

in your code for viewDidLoad, you need to set a frame for box after the view.addSubView(box)

This topic was automatically closed after 166 days. New replies are no longer allowed.