这是我第一次使用UIScrollViews采用纯Autolayout方法.这就是视图层次结构的样子 view-scrollview--view1--view2--view3 scrollview应按顺序包含view1 | view2 | view3. 我将scrollviews的宽度,高度,centerx和底部空间
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", @"easy", @"moderate", @"hard", @"very hard"]], [[PCCGenericRating alloc] initWithTitle:@"Difficulty" andMessage:@"YOu are not difficult at all" andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"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}.