Search code examples
iosswiftsubviewsuperview

Hidden subviews in UIViewController


Take this code:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var i = 0
        //let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
        //v1.backgroundColor = UIColor.redColor()
        //self.view.addSubview(v1)

        for v in self.view.subviews as! [UIView] {
            v.removeFromSuperview()
            i++
        }
        println(i)
    }
}

The for loop removes all subviews from the superview. At the end of the loop, i is 2, meaning there were 2 subviews removed. Since I didn't add any myself, what are these subviews?


Solution

  • There are two hidden layers in default UIViewController. When you print the output of your code you'll find

    <_UILayoutGuide: 0x7f981a513750; frame = (0 0; 0 0); hidden = YES; layer = > <_UILayoutGuide: 0x7f981a5142f0; frame = (0 0; 0 0); hidden = YES; layer = >