1

I have a collectionview and other views inside a scrollview. I need to add constraint programmatically to make sure it works on all devices.

CollectionView/Gallery is inside the UIScrollView. It works before the constraint.

I am using the code below to stick the collectionview to the right but I am getting error terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:

//constraint
NSLayoutConstraint *bottomSpaceConstraint = [NSLayoutConstraint constraintWithItem:self.galleryView
                                                                         attribute:NSLayoutAttributeRight
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:self.scrollView
                                                                         attribute:NSLayoutAttributeRight
                                                                        multiplier:1.0
                                                                          constant:0.0];
[self.galleryView addConstraint:bottomSpaceConstraint];
3
  • What is the relationship between galleryView and self.scrollView? Which is the subview, and has it been added as such by the time this code is run? Commented Dec 25, 2013 at 5:44
  • CollectionView/Gallery is inside the UIScrollView. Commented Dec 25, 2013 at 5:48
  • In that case, you have to add the constraint to the scrollView, not the galleryView. As an aside, I might be inclined to call the constraint variable rightSpaceConstraint, to make it clear its the right constraint, not the bottom constraint. Commented Dec 25, 2013 at 6:59

1 Answer 1

1

Constraints other than height and width must be added to the containing view or highest in the hierarchy.

You might find that scroll views don't make it easy or clear how to set up constraints for the items inside.

In that case, you either do the old school thing for scroll view content or you create an intermediate view inside the scroll view document view.

Scroll views are tricky because they have a clip view that determines how much of the document view is visible.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the old school way of doing it? If else?
Not using constraints inside the scrollview. It's ok to add them to items of the collection view to size them, but sometimes constraints and scrollviews are a pita. (at least on the desktop they certainly are)
I wouldn't be inclined to give up on constraints on scroll views, but just remember that constraints between a scroll view and its subviews dictate the contentSize of the scroll view, not its frame. But constraints with scroll views are a wonderful way to retire the complicated "old school" code to manually determine the contentSize of the scroll view. Just add the subviews and their constraints to the scroll view, and the contentSize will be calculated automatically for you.
I sure hope so. It hasn't always been that sweet with the NS variety of scrollviews. or is it???
For UIScrollView it's quite logical (once you realize that the subview's constraints dictate the contentSize) and auto layout can really simplify the process. With NSScrollView it is even more logical, because you're adding subviews to the documentView, so there's no confusion between the constraints between documentsView and its subviews (i.e. the logical equivalent to contentSize in UIScrollView) and the constraints on the NSScrollView itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.