使用ipAd的ios 6约束

时间:2013-02-22 02:19:25

标签: ios6 iad autolayout

我想使用约束来布局我的视图...... 它应该是这样的:

在肖像模式下: enter image description here

在横向模式下: enter image description here

到目前为止我的代码是用于约束的:

[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)...

1 个答案:

答案 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];
}