title: Different sets of auto layout constraints for different orientation
body: <p>I've seen similar <a href="https://stackoverflow.com/questions/20683698/how-to-configure-different-layouts-for-portrait-and-landscape-orientations-using">post</a> but it isn't really close to my case</p>
  
  <p>So I have different screen layouts for different orientations. App is using storyboards.
  To have a better understanding I attach photo of the UI sketch
  <img src="https://i.stack.imgur.com/ytWKv.jpg" alt="UI Sketch"></p>
  
  <p>As you can see there is a TableView and a Toolbar (a custom one). Toolbar contains 2 labels and a button.</p>
  
  <p>So portrait constraints are:</p>
  
  <ul>
  <li>Toolbar height 80pts</li>
  <li>Toolbar trailing and leading space == 0</li>
  <li>TableView's top space from navbar == 0</li>
  <li>TableView's bottom space from Toolbar = 0</li>
  <li>Tableview's width == Toolbar width</li>
  <li>Labels vertically centred inside toolbar, have fixed size and fixed distance from left and right</li>
  <li>Button has fixed sizes and centred inside toolbar</li>
  </ul>
  
  <p>Landscape constraints are totally different:</p>
  
  <ul>
  <li>Toolbar width 80pts</li>
  <li>Toolbar top (from navbar) and bottom space == 0</li>
  <li>Toolbar trailing space == 0</li>
  <li>TableView's leading space  == 0</li>
  <li>TableView's trailing space from Toolbar = 0</li>
  <li>Tableview's height == Toolbar width</li>
  <li>Labels horizontally centred inside toolbar, have fixed size and fixed distance from top and bottom</li>
  <li>Button has fixed sizes and centred inside toolbar</li>
  </ul>
  
  <p>I've tried to delete all constraints from storyboard and add them programmatically. But Xcode add them during compilation automatically. I've tried to delete all constraints inside root view but probably I didn't go properly through all views tree as several constrains aren't deleted.</p>
  
  <p>Do you have any idea how that can be implemented in a proper way?</p>
  
  <p>This is my first experience with Auto Layout and it isn't that easy as I expected.</p>
  
  <p>Here my solution which doesn't work (as Xcode adds some constraints during compilation)</p>
  
  <pre><code>// Those properties store all constraints for portrait and landscape modes
  @property (nonatomic, strong) NSArray *portraitConstraints;
  @property (nonatomic, strong) NSArray *landscapeConstraints;
  
  -(void)updateViewConstraints {
      [super updateViewConstraints];
  
      BOOL layoutIsPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
  
      if (layoutIsPortrait) {
          for (NSLayoutConstraint *constraint in self.landscapeConstraints)
              [constraint remove];
  
          for (NSLayoutConstraint *constraint in self.portraitConstraints)
              [constraint install];
      } else {
          for (NSLayoutConstraint *constraint in self.portraitConstraints)
              [constraint remove];
  
          for (NSLayoutConstraint *constraint in self.landscapeConstraints)
              [constraint install];
      }
  
  }
  
  - (NSArray *)portraitConstraints {
      if (!_portraitConstraints) {
          UIView *mainTable = self.myTableView;
          UIView *toolbar = self.toolbarView;
          UIView *firstLabel = self.firstLabel;
          UIView *secondLabel = self.secondLabel;
          UIView *bigButton = self.bigButton;
          UIView *navBar = self.navigationController.navigationBar;
  
          NSDictionary *views = NSDictionaryOfVariableBindings(mainTable,toolbar,firstLabel,secondLabel,bigButton,navBar);
  
          NSMutableArray *constraints = [NSMutableArray array];
          [constraints addObjectsFromArray:[NSLayoutConstraint
                                            constraintsWithVisualFormat:@"V:[toolbar(==80)]|"
                                            options:0
                                            metrics:nil
                                            views:views]];
          [constraints addObjectsFromArray:[NSLayoutConstraint
                                            constraintsWithVisualFormat:@"V:[navBar][mainTable][toolbar]|"
                                            options:0
                                            metrics:nil
                                            views:views]];
          // MORE AND MORE CONSTRAINTS ADDED HERE
  
          _portraitConstraints = constraints;
      }
      return _portraitConstraints;
  }
  </code></pre>