2

假设我有以下实现

  • UIPageViewController
  • UIScrollView称为mainScrollView添加subviewUIPageViewController.view

    [UIScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

  • UIScrollView使用子视图布局,NSLayoutConstraints并且每个子视图都已setTranslatesAutoresizingMaskIntoConstraints设置为 NO。

如何将内容大小设置UIScrollView为等于其子视图中最大的 MaxY 坐标?

这是我的代码示例:

UIScrollView *scrollView = [self mainScrollView];
UIView *superView = [self view];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView, superView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView(==superView)]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(==superView)]|" options:0 metrics: 0 views:viewsDictionary]];

UIView *previousView = scrollView;
for (int i=0;i<150; i++) {
    UITextView * textView = [[UITextView alloc] init];
    [textView setBackgroundColor:[UIColor colorWithRed:101.1f/255.f green:175.f/255.f blue:105.f/255.f alpha:1.f]];
    [textView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [scrollView addSubview:textView];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                           attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:previousView
                                                           attribute:NSLayoutAttributeTop
                                                          multiplier:1
                                                            constant:50]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                           attribute:NSLayoutAttributeLeft
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:textView.superview
                                                           attribute:NSLayoutAttributeLeft
                                                          multiplier:1
                                                            constant:0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                           attribute:NSLayoutAttributeWidth
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeWidth
                                                          multiplier:1
                                                            constant:0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                           attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeNotAnAttribute
                                                          multiplier:1
                                                            constant:40]];
    previousView = textView;
}
4

2 回答 2

0

我建议您使用 viewController viewDidLayoutSubviews来捕获约束求解器完成视图布局的点。然后获取最大 frame.origin.y 的 subView 并添加它的高度和可选的偏移量。像这样的东西:

scrollView.contentSize = CGSizeMake(superView.frame.size.width,maximumYView.origin.y + maximumYView.size.height + offset);
于 2013-01-15T01:59:54.143 回答
-1

你可以这样设置:

[mainScrollView setContentSize:CGSizeMake(yourWidth,yourHeight)];
于 2012-12-17T13:17:59.777 回答