How to initialise and add specific details about the view rectangle ?
We can create an instance of a view using the UIView initialiser and use the frame property to define the height, width and the x and y positions of the view (which is basically a rectangle). Finally we have to add it as a subview to a visible view so that we can see it
Code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1) //use color literal to get a variety of color options
let newView = UIView(frame: CGRect(x: 10, y: 100, width: 300, height: 50))
newView.backgroundColor = UIColor.white
view.addSubview(newView)
}
}
Output: