0
votes

I want to add scrolling imageviews to scrollviews using autolayout.

the view hierarchy as folllows:

---|

|---placeholder view

|---Scroll View

the code as follows:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString* identifier = @"WCollectionViewCell";


WCollectionViewCell* cell =[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imageScrollview.backgroundColor=[UIColor orangeColor];

UIImageView* imageview1 = [[UIImageView alloc]init];
imageview1.image =[UIImage imageNamed:@"1.jpg"];
imageview1.translatesAutoresizingMaskIntoConstraints=NO;
[cell.imageScrollview addSubview:imageview1];

NSDictionary* views = @{@"imageview1":imageview1,@"placeview":cell.placeHolderView,@"scrollview":cell.imageScrollview};

//set the imageview'size
NSArray *img_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageview1(placeview)]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:views];

NSArray *img_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageview1(placeview)]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:views];

[self.view addConstraints:img_constraint_H];
[self.view addConstraints:img_constraint_V];


//set the imageview's position
NSArray*  top_position = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageview1]"
                                                            options:0 metrics:nil views:views];

NSArray* bottom_position = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageview1]"
                                                              options:0 metrics:nil views:views];

[cell.imageScrollview addConstraints:top_position];
[cell.imageScrollview addConstraints:bottom_position];

return cell;

}

--------error---------- 2015-04-10 12:20:46.245 T[92361:1319616] The view hierarchy is not prepared for the constraint: When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug. 2015-04-10 12:20:46.246 T[92361:1319616] * Assertion failure in -[UIView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /SourceCache/UIKit_Sim/UIKit-3318.93/NSLayoutConstraint_UIKitAdditions.m:560 2015-04-10 12:20:46.249 T[92361:1319616] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Impossible to set up layout with view hierarchy unprepared for constraint.'

1

1 Answers

3
votes

Your problem is that you are adding the constraints for the height and width of the image to self.view which is not a parent view of the image.

You need to add the constraints to cell.imageScrollview, which is the cell superview or to the image itself.

So both of this should work:

//set the imageview'size
NSArray *img_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageview1(placeview)]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:views];

NSArray *img_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageview1(placeview)]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:views];

[cell.imageScrollview addConstraints:img_constraint_H];
[cell.imageScrollview addConstraints:img_constraint_V];

or

[imageview1 addConstraints:img_constraint_H];
[imageview1 addConstraints:img_constraint_V];

You have both options because the constraints don't reference the parent, just the imageView. If there was a | in the visual format, only the first option would work.

One more tip:

You could add all your constraints at once:

WCollectionViewCell* cell =[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imageScrollview.backgroundColor=[UIColor orangeColor];

UIImageView* imageview1 = [[UIImageView alloc]init];
imageview1.image =[UIImage imageNamed:@"1.jpg"];
imageview1.translatesAutoresizingMaskIntoConstraints=NO;
[cell.imageScrollview addSubview:imageview1];

NSDictionary* views = @{@"imageview1":imageview1,@"placeview":cell.placeHolderView,@"scrollview":cell.imageScrollview};    
//set the imageview's position
NSArray*  horizontal_constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageview1(placeview)]"
                                                            options:0 metrics:nil views:views];

NSArray* vertical_constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageview1(placeview)]"
                                                              options:0 metrics:nil views:views];

[cell.imageScrollview addConstraints:horizontal_constraints];
[cell.imageScrollview addConstraints:vertical_constraints];

return cell;

Let me know how it goes, or if you need more help!