假设我有以下实现:
UIPageViewController
UIScrollView
称为mainScrollView添加subview
到UIPageViewController.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;
}