vIEw-scrollvIEw--vIEw1--vIEw2--vIEw3
scrollvIEw应按顺序包含vIEw1 | vIEw2 | vIEw3.
我将scrollvIEws的宽度,高度,centerx和底部空间设置为supervIEw.创建的vIEw1,vIEw2和vIEw3都在其updateConstraints方法中设置了宽度和高度约束.另外,代码中提供了一些约束.这个scrollvIEw不是从左向右滚动的原因是什么?我已经在网上找到了所有可以在网上找到的关于使用自动布局以编程方式创建和添加子视图到UIScrollVIEw的指南.我发现有一些提及必须提供四个不同的约束,每个视图的前导,尾随,顶部和底部作为子视图添加到scrollvIEw.这些是唯一可以指定的NSLayoutAttributes吗? NSLayoutAttribueleft或NSLayoutAttribueRight等属性如何关联?我也在苹果网站上阅读了文档,特别是https://developer.apple.com/library/ios/technotes/tn2154/_index.html.我附上了我目前的设置.一切都是通过代码完成的.
- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; self.dataSource = @[ [[PCCGenericrating alloc] initWithTitle:@"Easiness" andMessage:@"WHAT A JOKERRRR" andVariatons:@[ @"very easy",@"easy",@"moderate",@"hard",@"very hard"]],[[PCCGenericrating alloc] initWithTitle:@"Joker" andMessage:@"WHAT A JOKERRRR" andVariatons:@[ @"very easy",[[PCCGenericrating alloc] initWithTitle:@"Difficulty" andMessage:@"YOu are not difficult at all" andVariatons:@[ @"very easy",@"very hard"]] ]; [self initVIEw];}- (voID)initVIEw { CGfloat navigationbarHeight = self.navigationController.navigationbar.frame.size.height; CGfloat statusbarHeight = [[UIApplication sharedApplication] statusbarFrame].size.height; CGfloat heightDifference = navigationbarHeight + statusbarHeight; self.scrollVIEw = [[UIScrollVIEw alloc] init]; self.scrollVIEw.delegate = self; [self.scrollVIEw setTranslatesautoresizingMaskIntoConstraints:NO]; self.scrollVIEw.backgroundcolor = [UIcolor greencolor]; [self.vIEw addSubvIEw:self.scrollVIEw]; //setup constraints [self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollVIEw attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeWIDth multiplIEr:1.0f constant:0.0f]]; [self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollVIEw attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeHeight multiplIEr:1.0f constant:-heightDifference]]; [self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollVIEw attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeCenterX multiplIEr:1.0f constant:0.0f]]; [self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollVIEw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeBottom multiplIEr:1.0f constant:0.0]]; [self.dataSource enumerateObjectsUsingBlock:^(ID obj,NSUInteger IDx,BOol *stop) { PCCGenericrating *rating = (PCCGenericrating *)obj; PCCGenericratingVIEw *ratingVIEw = [self createVIEwWithrating:rating]; [self.scrollVIEw addSubvIEw:ratingVIEw]; int multiplIEr = (IDx == 0) ? 1 : (int) (IDx + 1) ; [self.scrollVIEw addConstraint:[NSLayoutConstraint constraintWithItem:ratingVIEw attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scrollVIEw attribute:NSLayoutAttributeCenterX multiplIEr:multiplIEr constant:0.0f]]; [self.scrollVIEw addConstraint:[NSLayoutConstraint constraintWithItem:ratingVIEw attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scrollVIEw attribute:NSLayoutAttributeCenterY multiplIEr:1.0f constant:0.0f]]; }];}- (PCCGenericratingVIEw *)createVIEwWithrating:(PCCGenericrating *)rating { PCCGenericratingVIEw *vIEw = [PCCGenericratingVIEw genericratingVIEwWithTitle:rating.Title andMessage:rating.message]; return vIEw;}
打印出scrollvIEw约束后,它们对我来说没问题:
po self.scrollVIEw.constraints<__NSArrayM 0x115b051f0>(<NSLayoutConstraint:0x1145d9290 PCCGenericratingVIEw:0x114579880.centerX == UIScrollVIEw:0x11458d4b0.centerX>,<NSLayoutConstraint:0x1145d9410 PCCGenericratingVIEw:0x114579880.centerY == UIScrollVIEw:0x11458d4b0.centerY>,<NSLayoutConstraint:0x1145d9dd0 PCCGenericratingVIEw:0x1145d9560.centerX == 2*UIScrollVIEw:0x11458d4b0.centerX>,<NSLayoutConstraint:0x1145d9e40 PCCGenericratingVIEw:0x1145d9560.centerY == UIScrollVIEw:0x11458d4b0.centerY>,<NSLayoutConstraint:0x1145da6b0 PCCGenericratingVIEw:0x1145d9e90.centerX == 3*UIScrollVIEw:0x11458d4b0.centerX>,<NSLayoutConstraint:0x1145da730 PCCGenericratingVIEw:0x1145d9e90.centerY == UIScrollVIEw:0x11458d4b0.centerY>)
以下是它的外观截图:
我觉得奇怪的是数据源中的最后一个元素是滚动视图中显示的第一个视图控制器,它应该是最后一个视图.它也不会按原样从左向右滚动.
解决方法 确保vIEw3的vIEw1和bottom_constraint的top_constraint符合scrollVIEw的约束.否则scrollvIEw的contentSize:{0,0}. 总结以上是内存溢出为你收集整理的iOS – Pure AutoLayout和UIScrollView不滚动全部内容,希望文章能够帮你解决iOS – Pure AutoLayout和UIScrollView不滚动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)