Sometimes the playground does not work as expected. This is probably because both views do not have specific superviews.
In a real application, it works great.
override func viewDidLoad() {
super.viewDidLoad()
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 37.5, height: 66.7))
containerView.backgroundColor = UIColor.redColor()
view.addSubview(containerView)
let myView = UIView()
myView.frame = CGRect(x: 0, y: 290, width: 200, height: 200)
myView.backgroundColor = UIColor.blueColor()
view.addSubview(myView)
}
I tried this code in playgorund:
let cv = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
cv.backgroundColor = UIColor.redColor()
let v1 = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 30.0, height: 60.0))
v1.backgroundColor = UIColor.yellowColor()
cv.addSubview(v1)
let v2 = UIView()
v2.frame = CGRect(x: 0, y: 200, width: 30.0, height: 60.0)
v2.backgroundColor = UIColor.blueColor()
cv.addSubview(v2)
and it works great for both initializers.
source
share