UIView not showing elements in ViewController Swift 4

I have a UIView class. And assigned it to a UIView inside of a ViewController.

I am animating the drawing of a circle in the UIView.

How do I load the UIView in the ViewController?

ViewDidLoad doesn’t work or gives errors. LoadView also doesn’t work.

2 answers

  • answered 2018-08-16 05:33 teja_D

    Create class for your view controller lets say ParentController subclass of UIViewController.

    Create outlet of your UIView in ParentController.

    Instead of creating class for UIView define function in ParentController to draw circle and call it in ParentController.

    After function call show your add your UIView to ParentController e.g - self.view.addSubview(self.cirecleUIview)

  • answered 2018-08-16 10:20 ingconti

    I personally suggest

    1) create a file for your class, say MyView.

    2) create a NIB/XIB, too, so you can eventually add other elements.

    3) add a custom element (of class MyView) in controller in Main storyboard

    4) Add outlet in your controller to your view (so you can fill/set/access it..)

    5) NEVER call directly drawing functions (will be automatic.. or triggered manually using setNeedsUpdate..)

    6) override init/awake methods, so you can load from resources or code:

    override init(frame: CGRect) {
        super.init(frame: frame)
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    final private func prepare(){
        // your "init" code here..

    6) implement you drawing code:

    override func draw(_ rect: CGRect) {
        // stupid code.. will fill in red...
    if let context = UIGraphicsGetCurrentContext(){