UIView on the playground

I defined two species differently, for example -

First method:

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
containerView.backgroundColor = UIColor.redColor()

I see a rectangle.

Second method:

let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
view.backgroundColor = UIColor.blueColor()

I see some kind of log message, such as output.

Why do both give different outputs?

Some textbooks on the Internet use the first method and the second.

Here is the image.

enter image description here

+4
source share
1 answer

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.

0
source

Source: https://habr.com/ru/post/1650494/


All Articles