1

I would like to use constraints to layout my view... It should look like this :

In Portrait mode : enter image description here

In Landscape mode: enter image description here

My code so far is for constraint:

[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)]];

If I do not put the (50) aside of _ad, the _tableView will be over it.

and of course, with the (50) aside of _ad, when to landscape, it won't move up the tableView 18px higher (since the Ad will then be 480x32)...

1 Answer 1

1

You need to set the height of the "ad" when the orientation changes:

- (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];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.