uiscrollview的autolayout,视图层级没有准备约束

uiscrollview's autolayout,The view hierarchy is not prepared for the constraint

我想使用自动布局将滚动图像视图添加到滚动视图。

视图层次结构如下:

---|

|---占位符视图

|---滚动视图

代码如下:

-(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;

}

--------错误---------- 2015-04-10 12:20:46.245 T[92361:1319616] 未为约束准备视图层次结构: 添加到视图时,约束项必须是该视图(或视图本身)的后代。如果在组装视图层次结构之前需要解决约束,这将崩溃。中断 -[UIView _viewHierarchyUnpreparedForConstraint:] 进行调试。 2015-04-10 12:20:46.246 T[92361:1319616] * -[UIView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /SourceCache/UIKit_Sim/UIKit-3318.93/[=38= 断言失败]:560 2015-04-10 12:20:46.249 T[92361:1319616] * 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因:'Impossible to set up layout with view hierarchy unprepared for constraint.'

您的问题是您将图像的高度和宽度约束添加到 self.view,这不是图像的父视图。

您需要将约束添加到 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];

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

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

您有两种选择,因为约束不引用父级,仅引用 imageView。如果视觉格式中有 |,则只有第一个选项有效。

还有一个提示:

您可以一次添加所有约束:

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;

让我知道进展如何,或者如果您需要更多帮助!