1

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

在纵向模式下: 在此处输入图像描述

在横向模式下: 在此处输入图像描述

到目前为止,我的代码是为了约束:

[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 (因为 Ad 将是 480x32)...

4

1 回答 1

1

当方向改变时,您需要设置“广告”的高度:

- (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];
}
于 2013-02-22T04:23:32.777 回答