首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >uiscrollview的自动收费表,视图层次结构没有为约束做好准备

uiscrollview的自动收费表,视图层次结构没有为约束做好准备
EN

Stack Overflow用户
提问于 2015-04-10 04:30:35
回答 1查看 3.8K关注 0票数 0

我希望使用自动收费表向滚动视图添加滚动图像视图。

视图层次如下:

--占位符视图

-涡旋视图

守则如下:

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

代码语言:javascript
复制
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;

}

-错误如果需要在组装视图层次结构之前解决约束,这将导致崩溃。中断-UIView _viewHierarchyUnpreparedForConstraint:进行调试。2015-04-10 12:20:46.246 T 92361:1319616 _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:,/SourceCache/UIKit_Sim/UIKit-3318.93/NSLayoutConstraint_UIKitAdditions.m:560 *断言失败在-UIView 2015-10 12:20:46.249 T 92361:1319616 *终止应用程序中由于“NSInternalInconsistencyException”异常,原因:“不可能设置视图层次结构未为约束做好准备的布局。”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-14 08:57:06

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

您需要将约束添加到cell.imageScrollview,即单元格超级视图或图像本身。

因此,这两种方法都应该有效:

代码语言:javascript
复制
//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];

代码语言:javascript
复制
[imageview1 addConstraints:img_constraint_H];
[imageview1 addConstraints:img_constraint_V];

这两个选项都有,因为约束不引用父程序,而只引用imageView。如果在视觉格式中有一个|,那么只有第一个选项可以工作。

还有一条建议:

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

代码语言:javascript
复制
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;

如果你需要更多的帮助,请告诉我事情进展如何!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29553410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档