Reputation: 2612
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?
Upvotes: 0
Views: 151
Reputation: 6472
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 = >
Upvotes: 2
Reputation: 348
A UIViewController's view property can have some internal subviews used by the system, and these can vary based on iOS version, context, etc. If you want to remove a subview that you yourself created, one simple way to do this is to declare your v1 UIView as a property of your ViewController class
class ViewController: UIViewController {
let v1 = UIView(frame:CGRectMake(113, 111, 132, 194))
override func viewDidLoad() {
super.viewDidLoad()
//add the view
v1.backgroundColor = UIColor.redColor()
self.view.addSubview(v1)
//remove the view
v1.removeFromSuperview()
}
}
Upvotes: 0