iOS Swift Draw Simple Graphics

Hi everyone. In this tutorial, we will learn how to draw simple graphic images for our iOS Swift project using pure code.

Open your Xcode IDE and create a new project.

In the ViewController.swift, add these codes into func viewDidLoad().

let DynamicView = UIView(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
DynamicView.backgroundColor = UIColor.green
DynamicView.layer.cornerRadius = 25
DynamicView.layer.borderWidth = 2
self.view.addSubview(DynamicView)

Here’s the complete code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
//  ViewController.swift
//  Hello World
//
//  Created by Farizshah Gaskin on 28/11/2018.
//  Copyright © 2018 Fariz Gaskin Technologies. All rights reserved.
//
 
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
 
        let DynamicView = UIView(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
        DynamicView.backgroundColor = UIColor.green
        DynamicView.layer.cornerRadius = 25
        DynamicView.layer.borderWidth = 2
        self.view.addSubview(DynamicView)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
}

Build and Run the app to see the results

Conclusion

We hope you enjoyed this quick reference. See you next time!

Leave a Reply

Your email address will not be published. Required fields are marked *