我对自动布局有一些问题。
我们有带有根视图和子视图的 ViewController。子视图具有固定的纵横比。我需要在旋转时将子视图放入父视图中。此外,儿童视图应居中。就像在图片上:
我有这个代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * v = [UIView new];
v.backgroundColor = [UIColor redColor];
v.translatesAutoresizingMaskIntoConstraints = NO;
v.tag = 100;
[self.view addSubview:v];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|"
options:0
metrics:nil
views:@{@"v":v}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|"
options:0
metrics:nil
views:@{@"v":v}]];
for (NSLayoutConstraint *c in self.view.constraints) {
[c setPriority:800];
}
NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:v
attribute:NSLayoutAttributeWidth
multiplier:0.8
constant:0.];
[c setPriority:1000];
[v addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:v
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0.];
[c setPriority:1000];
[self.view addConstraint:c];
}
而且它不起作用,它缩小了景观的超级视野。