uiscrollview的自动布局,视图层次结构没有为约束做好准备
Posted
技术标签:
【中文标题】uiscrollview的自动布局,视图层次结构没有为约束做好准备【英文标题】:uiscrollview's autolayout,The view hierarchy is not prepared for the constraint 【发布时间】:2015-04-10 04:30:35 【问题描述】:我想使用自动布局将滚动图像视图添加到滚动视图。
视图层次结构如下:
---|
|---占位符视图
|---滚动视图
代码如下:
-(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/NSLayoutConstraint_UIKitAdditions.m:560 2015-04-10 12:20:46.249 T[92361:1319616] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法设置未准备好约束的视图层次结构的布局。”
【问题讨论】:
【参考方案1】:您的问题是您将图像的高度和宽度的约束添加到 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;
如果您需要更多帮助,请告诉我进展情况!
【讨论】:
以上是关于uiscrollview的自动布局,视图层次结构没有为约束做好准备的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在情节提要中使用 uiscrollview 设计视图并检查自动布局?