macawm
macawm

Reputation: 142

Mysterious NSLayoutConstraints causing conflicts

I am using MGSplitViewClass (link) and I'm having severe difficulty getting the size of a UICollectionView correctly set. In said split view I have in the master view a set of four UIViews that contain more robust views (table views, groups of labels, and a collection view).

First I tried to add the UICollectionView in my nib and programmatically created a collection view controller and connect it to my nib's collection view. This failed horribly as soon a I tried _collectionViewController.collectionView = myCollectionView;. This gave errors about the view from my nib being outside of the subtree 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal.

Fine, now I'll remove the collection view from my nib and add the one the collection view controller comes with, like so.

_collectionViewController = [[[MyCollectionViewController alloc] initWithCollectionViewLayout: [[[MyCollectionLayout alloc] init] autorelease]] retain];
[_collectionViewController.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
UICollectionView* collectionView = _attachmentController.collectionView;
[_collectionViewContainer addSubview:collectionView];

This works but the collection view is overflowing the view container's size significantly and other layout issues. Now obviously the view will not scroll properly like this so I have to fix it.

So I add some constraints to get the view sized to fill the container view.

NSMutableArray* constraints = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[collectionView]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(collectionView)] mutableCopy];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[collectionView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(collectionView)]];
for (NSLayoutConstraint* lc in constraints)
    [_attachmentViewContainer addConstraint:lc];

But this is where it gets screwy. Upon running this I get more layout errors.

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
"<NSAutoresizingMaskLayoutConstraint:0x1d8c0b80 h=-&- v=-&- UICollectionView:0x1cb55400.midX == UIView:0x1d894300.midX - 10>",
"<NSLayoutConstraint:0x1c5a74b0 H:[UICollectionView:0x1cb55400]-(0)-|   (Names: '|':UIView:0x1d894300 )>",
"<NSLayoutConstraint:0x1c5a7440 H:|-(0)-[UICollectionView:0x1cb55400]   (Names: '|':UIView:0x1d894300 )>"

and

"<NSAutoresizingMaskLayoutConstraint:0x1d8c0d10 h=-&- v=-&- UICollectionView:0x1cb55400.midY == UIView:0x1d894300.midY + 437>",
"<NSLayoutConstraint:0x1c5a71a0 V:[UICollectionView:0x1cb55400]-(0)-|   (Names: '|':UIView:0x1d894300 )>",
"<NSLayoutConstraint:0x1c5a6f30 V:|-(0)-[UICollectionView:0x1cb55400]   (Names: '|':UIView:0x1d894300 )>"

Where did those random looking NSAutoresizingMaskLayoutConstraints come from? A better question is where can I get rid them? I cannot seem to find a single point in the code to jump in a kill them. -viewWillAppear: is not propagated past UIViewController so I'm sort of stuck.

Any thoughts are very welcome.

Upvotes: 0

Views: 3209

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Just before you add those constraints, try adding this line:

[collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];

The system will automatically translate the default autoresizing mask to constraints unless you set this to NO.

Upvotes: 2

Related Questions