Hello!

There is a screen with a segmented control that has 2 states. In the picture you can see everything:

alt text

Autolayout enabled. So, how to set constraints for the UITableView upper border (marked in yellow) so that for the first state, the table is bound to the navigation bar, and in the second state - to the UIView (view with From-to-Where elements).

I tried for each state to manually register tableView.frame = CGRectMake ..., but this does not work.

    2 answers 2

    Here is a good example: Creating UIViews programmatically with Auto Layout

    Since Xcode 4.0, I have apps with Interface Builder (IB). First with xibs and recently with Storyboards. I’m a fan of IB and haven’t been able to criticize Xcode 4.x version of IB. I have been able to avoid any problems.

    • Thank you for the answer, but it does not work :( Please see below, in addition to the post. - michilly

    I tried the following: when loading a view, I create constraints depending on the state:

    - (void)setCityConstraints { NSLog(@"set city constraints"); tView.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *myConstraint =[NSLayoutConstraint constraintWithItem:self.tView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; [self.view addConstraint:myConstraint]; } - (void)setInterCityConstraints { NSLog(@"set intercity constraints"); tView.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *myConstraint =[NSLayoutConstraint constraintWithItem:self.tView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:50.0]; [self.view addConstraint:myConstraint]; } 

    At the first load, everything works fine, but if I start changing the state of the view by changing the segmentedControl, then the second form of the tableView fits on the UIView (where the buttons are From and Where to).

     - (IBAction)orderTypeChanged:(id)sender { if (orderTypeControl.selectedSegmentIndex == 0) { [self showCity]; [self setCityConstraints]; } else { [self showIntercity]; [self setInterCityConstraints]; } } 
    • Try adding consraints not to the navbar, and to topLayoutGuide, ie [NSLayoutConstraint constraintWithItem: self.view attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: self.topLayoutGuide attribute: NSLayoutAttributeTop // can and bottom, not sure if you need to play around with that thing multiplier: 1.0 constant: 50.0]; - iFreeman
    • You can not do this in xib? In my experience, this solution is usually - iFreeman
    • still please describe if messages about invalid constraints are displayed in the logs? - iFreeman pm
    • Logs are deduced that just these lines are wrong and will be broken. Therefore, I had to make crutches: I bring info to 2 UITableView: I have my own table for each state - michilly