elliotrock
elliotrock

Reputation: 3420

Programatically Autolayout with UITableViewController in a UIViewController

I have scourge all of the Autolayout questions without luck to my simple question:

I have a UIViewController displayed in a UINavigationController. That UIViewController has a UITableViewController in it. This is the code:

FirstViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *contentView = [UIView new];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    self.view = contentView;
    [self.view setBackgroundColor:[UIColor redColor]];
    firstTable=[[FirstTableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self.view addSubview:firstTable.tableView];

}

Now when I run that I get gap between the top of the FirstViewController and the tableViewController (which has no position code in it).

enter image description here

SO using the viewDidLayoutSubviews Method I have tried various contraints

-(void) viewDidLayoutSubviews
{
[firstTable.tableView layoutIfNeeded];
//[firstTable.tableView pinEdge:NSLayoutAttributeTop toEdge:NSLayoutAttributeTop ofView:self.view inset:0.0]; 
// CRASHES
[firstTable.tableView addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.0]]; 
// CRASHES: A constraint cannot be made that sets a location equal to a constant. Location attributes must be specified in pairs'

}

I have tried a few different methods above

I just want to match the top of tableView to the view of the ViewController. The issue with setting a frame is I have had issues with fullscreen view (video player) pushing this gap back.

How do I match the top of this tableViewController to remove this gap? Programmatically, NOt using IB using iOS 7 Autolayout features please!

Solution

Sir Lord High Stack Master @jrturton pointed me in a direction that I was considering, in general the structure above is the issue. I will try his general pointers but I consider it a structural issue and moved the delegate and datasource functionality out of the original VC and brought that into the UITableViewController, which is what it is meant to be used as!

Upvotes: 0

Views: 1493

Answers (1)

jrturton
jrturton

Reputation: 119242

  1. Setting self.view in viewDidLoad is... odd. By the time that method is called, self.view is already set.
  2. The main view property of a view controller doesn't need anything doing to its auto resizing masks or anything. If I'm building a view manually I usually just do self.view = [UIView new]; in loadView.
  3. If you're adding a child view controller, make sure you add it as a child view controller, don't just add the view
  4. If you want to lay out a child view controller using autolayout, you need to set translatesAutoresizingMaskIntoConstraints to NO on the child view controller's view.
  5. viewDidLayoutSubviews isn't a good place to be creating constraints. Fixed constraints should be added when the sub view is added, so you should probably do everything in loadView - once you've followed the advice above, just pin the table view to the top, left and right edges (and set a height if it doesn't reach all the way down, otherwise pin to the bottom as well, but if you're doing that I'm not sure why it's a child view controller).

Upvotes: 2

Related Questions