create a view and make its background colour red in swift

In order to create a UIView and set its background color to red, follow these steps:

  1. First, create an instance of UIView:
main.swift
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
72 chars
2 lines
  1. Then, set its background color to red:
main.swift
myView.backgroundColor = .red
30 chars
2 lines
  1. You can also add it to your view hierarchy:
main.swift
view.addSubview(myView)
24 chars
2 lines

Here's the complete code:

main.swift
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
myView.backgroundColor = .red
view.addSubview(myView)
126 chars
4 lines

gistlibby LogSnag