我UIViewController
通过覆盖 loadView 方法来创建它的视图:
- (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.
那么,正确的 Contraints 应该是什么样子的呢?