UIViewControllerのtopLayoutGuideプロパティを参照する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
self.upperView.translatesAutoresizingMaskIntoConstraints = NO; self.lowerView.translatesAutoresizingMaskIntoConstraints = NO; NSNumber *vPadding = @0; NSNumber *hPadding = @0; id topGuide = self.topLayoutGuide; UIView *upperView = self.upperView; UIView *lowerView = self.lowerView; NSDictionary *views = NSDictionaryOfVariableBindings(topGuide, upperView, lowerView); NSDictionary *metricsDictionary = NSDictionaryOfVariableBindings(vPadding, hPadding); NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[vPadding]-[topGuide]-[upperView(100)]-[lowerView]-[vPadding]-|" options:0 metrics:metricsDictionary views:views]; [self.view addConstraints:vConstraints]; NSArray *hConstraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[hPadding[-[upperView]-[hPadding]-|" options:0 metrics:metricsDictionary views:views]; [self.view addConstraints:hConstraints1]; NSArray *hConstraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[hPadding[-[lowerView]-[hPadding]-|" options:0 metrics:metricsDictionary views:views]; [self.view addConstraints:hConstraints2]; [self.view layoutIfNeeded]; |
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/