loadViewUIViewController
メソッドを上書きしてビューを作成します。
- (void)loadView {
UIView *view = [[UIView alloc] init];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
}
今、私は AutoLayout に切り替えたいので、
view.translatesAutoresizingMaskIntoConstraints = NO;
loadView メソッドに。ここで、以前に自動生成されたのと同じ制約を指定する必要があります。私のアプローチは、 updateViewConstraints を上書きすることでした
- (void)updateViewConstraints {
if (0 == [[self.view constraints] count]) {
NSDictionary* views = @{@"view" : self.view};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:0 views:views]];
}
[super updateViewConstraints];
}
ただし、この種の制約はスーパー ビューに適用する必要があると思うため、例外が発生します。
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal.
では、正しい制約はどのように見える必要があるのでしょうか?