Skip to content

Commit e8e89c7

Browse files
committed
added more logging to make sure we understand how default layout margins work
1 parent a93fa54 commit e8e89c7

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

bk2ch01p033layoutMargins/bk2ch01p033layoutMargins/ViewController.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
import UIKit
33

4+
func delay(_ delay:Double, closure:@escaping ()->()) {
5+
let when = DispatchTime.now() + delay
6+
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
7+
}
8+
9+
410
class ViewController: UIViewController {
511

612
var didSetup = false
@@ -12,19 +18,36 @@ class ViewController: UIViewController {
1218

1319
let mainview = self.view!
1420

15-
let v = UIView()
21+
// ok, I proved what I wanted to prove: it really _is_ whether it's a vc's main view
22+
// let vc = UIViewController()
23+
// self.addChildViewController(vc)
24+
25+
let v = UIView()
26+
// let v = vc.view!
1627
v.backgroundColor = .red
1728
v.translatesAutoresizingMaskIntoConstraints = false
1829

1930
mainview.addSubview(v)
2031

32+
// vc.didMove(toParentViewController: self)
33+
2134
NSLayoutConstraint.activate([
2235
NSLayoutConstraint.constraints(withVisualFormat:"H:|-(0)-[v]-(0)-|", metrics: nil, views: ["v":v]),
2336
NSLayoutConstraint.constraints(withVisualFormat:"V:|-(0)-[v]-(0)-|", metrics: nil, views: ["v":v])
2437
].flatMap{$0})
2538

2639
// experiment by commenting out this line
27-
v.preservesSuperviewLayoutMargins = true
40+
// v.preservesSuperviewLayoutMargins = true
41+
delay(1) {
42+
// print(mainview.layoutMargins)
43+
// 20 on iPad Pro large
44+
// 20 on iPhone 6s Plus
45+
// 20 on iPad Air 2
46+
// 16 on iPhone 5s
47+
// 16 on iPhone 6s
48+
print(v.layoutMargins)
49+
// print(v.window!.layoutMargins)
50+
}
2851

2952
let v1 = UIView()
3053
v1.backgroundColor = .green

0 commit comments

Comments
 (0)