假设我有以下实施:
UIPageViewController
UIScrollView
添加为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;
}
答案 0 :(得分:0)
我建议你使用viewController viewDidLayoutSubviews 来捕获约束求解器完成布局视图的点。然后获取具有最大frame.origin.y的subView并添加其高度和可选的偏移量。像这样:
scrollView.contentSize = CGSizeMake(superView.frame.size.width,maximumYView.origin.y + maximumYView.size.height + offset);
答案 1 :(得分:-1)
您可以这样设置:
[mainScrollView setContentSize:CGSizeMake(yourWidth,yourHeight)];