uiscrollview的autolayout,视图层次结构没有为约束准备

时间:2015-04-10 04:30:35

标签: ios objective-c uiscrollview autolayout

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

视图层次结构如下:

--- |

| ---占位符视图

| ---滚动视图

代码如下:

- (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:mutualExclusiveConstraints:],/ SourceCache / UIKit_Sim / UIKit-3318.93 / NSLayoutConstraint_UIKitAdditions.m:560 2015-04-10 12:20:46.249 T [92361:1319616] * 由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法使用视图层次结构设置布局没有准备好约束。'

1 个答案:

答案 0 :(得分:3)

您的问题是您要将图像高度和宽度的约束添加到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; ,则只有第一个选项可用。

还有一个提示:

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

{{1}}

让我知道它是怎么回事,或者你是否需要更多的帮助!