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.'