我想使用约束来布局我的视图...... 它应该是这样的:
在肖像模式下:
在横向模式下:
到目前为止我的代码是用于约束的:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[profilHeader]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(profilHeader)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_ad]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_ad)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_tableView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[profilHeader(100)][_ad(50)][_tableView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(profilHeader, _ad, _tableView)]];
如果我不把(50)放在_ad之外,那么_tableView就会覆盖它。
当然,除了_ad之外的(50),当横向时,它不会向上移动tableView 18px更高(因为广告将是480x32)...
答案 0 :(得分:1)
你需要设置" ad"的高度。当方向改变时:
- (void)updateViewConstraints{
[super updateViewConstraints];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_profilHeader]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_profilHeader)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_ad]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_ad)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_tableView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_profilHeader(100)][_ad][_tableView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_profilHeader, _ad, _tableView)]];
//remove the existing constraint
[self.view removeConstraint:self.heightConstraint];
//set the height according to orientation
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
self.heightConstraint=[NSLayoutConstraint constraintWithItem:_ad attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:0 multiplier:1.0 constant:50];
}
//landscape
else{
self.heightConstraint=[NSLayoutConstraint constraintWithItem:_ad attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:0 multiplier:1.0 constant:32];
}
[self.view addConstraint:self.heightConstraint];
}