Reputation: 3420
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).
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
Reputation: 119242
self.view
in viewDidLoad
is... odd. By the time that method is called, self.view is already set. self.view = [UIView new];
in loadView
. translatesAutoresizingMaskIntoConstraints
to NO
on the child view controller's view. 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