Skip to content

Instantly share code, notes, and snippets.

@maxkramer
Created October 14, 2014 18:46
Show Gist options
  • Save maxkramer/fc028c78bc2a92cab8eb to your computer and use it in GitHub Desktop.
Save maxkramer/fc028c78bc2a92cab8eb to your computer and use it in GitHub Desktop.
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:-CGRectGetHeight(self.searchBar.frame)];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.tableView setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.view addConstraint:widthConstraint];
[self.view addConstraint:heightConstraint];
[self.view addConstraint:leftConstraint];
[self.view addConstraint:bottomConstraint];
@algal
Copy link

algal commented Oct 14, 2014

Hmm.. Has gist mistaken some of the log message for markup and removed it? The array of conflicting constraints just has empty strings, as does the statement about which constraint it is removing.

@algal
Copy link

algal commented Oct 14, 2014

Is the search bar always present? If it is, what you should be doing is just setting a spacer constraint so that the maxY of the search bar (its bottom) equals the minY of the tableview (its top). That will be less fragile than relying on capturing its frame.height at the right moment, or constraining against the top layoutGuide. That should resolve the y position. I'm not sure yet about the height and width. Let me wait and see what the logs say.

@maxkramer
Copy link
Author

@algal it has indeed, I didn't notice. I've wrapped it in a code-block so it can now be read! It's a shame that the comment editor doesn't have a more intuitive interface...

@algal
Copy link

algal commented Oct 14, 2014

I notice the search bar also has the wrong width. So it's layout is being managed by whatever is managing the tableView layout. Really need to understand the VH better.

@algal
Copy link

algal commented Oct 14, 2014

have you enabled auto layout in your xib or storyboard?

@maxkramer
Copy link
Author

That's true, I've screenshotted the size inspector of both UI elements, and the only auto-layout related issue that could arise, as far as I'm aware, could be something to do with the intrinsic content size?

Size Inspector: http://cl.ly/image/3e1E2T0z2z3l
View Layout: http://cl.ly/image/3P1R2R2C1Z30

@maxkramer
Copy link
Author

Yes, auto layout is enabled in the Storyboard.

@algal
Copy link

algal commented Oct 14, 2014

(So the log message about NSIBPrototypingLayoutConstraint indicates that IB is automatically generating a height=108 constraint that conflicts with the one you are trying to create.)

If you have enabled auto layout in the storyboard, then how come Document Outline in IB screenshots do not show the constraints? What version of IB is this?

@algal
Copy link

algal commented Oct 14, 2014

Hang on. I know the answer to that one. let me think a minute.

@algal
Copy link

algal commented Oct 14, 2014

For now, try this: in the view inspector, for the tableview, select the checkbox for the intrinsic size being "Default (System Defined)", and change it. You will see checkboxes that let you designate that the height and width are placeholders. Check those boxes. That will tell IB not to auto-generate those constraints based on the geometry values you see now in the view inspector. It tells it that the current values you are using are just "placeholders".

This might help but I don't think this will solve everything. If the whole project did not use AL, and then you enable AL, there may be more housekeeping to do to transition. I'm not sure off the top of my head.

@algal
Copy link

algal commented Oct 14, 2014

The reason you don't see any of the blue constraints boxes I was expecting to see in the Document Outliner, is that although you've enabled constraints you haven't created any in IB. So IB is auto-generating constraints based on the current AR and frame values everywhere.

@maxkramer
Copy link
Author

It sounds like it will work as the constraints shouldn't be generated in that case, I just need to get past Xcode (6.0.1) crashing when I check the checkboxes for the table view.

@algal
Copy link

algal commented Oct 14, 2014

I find erasing ~/Library/Developer/Xcode/DerivedData/projectname-blahblah/ often works wonders.

@maxkramer
Copy link
Author

I've made the suggested changes, which didn't affect the layout either. I have however tracked back the IB generated constraint to being a result of the Y value of the table view not being 0 initially. The fix for this would of course be setting the Y value to 0, but this didn't get rid of the constraint.

Ha, I had a derived data exterminator from Alcatraz, but I haven't got around to reinstalling it yet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment