如何自动调整UIScrollView的大小以适应内容?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (54)

有没有办法UIScrollView自动调整其滚动内容的高度(或宽度)?

类似于:

[scrollView setContentSize:(CGSizeMake(320, content.height))];
提问于
用户回答回答于

UIScrollView根据其所载:

objc

CGRect contentRect = CGRectZero;

for (UIView *view in self.scrollView.subviews) {
    contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;

Swift

  var contentRect = CGRect.zero

  for view in mainScrollView.subviews {
     contentRect = contentRect.union(view.frame)
  }
  mainScrollView.contentSize = contentRect.size
用户回答回答于

UIScrollView

CGFloat scrollViewHeight = 0.0f;
for (UIView* view in scrollView.subviews)
{
   scrollViewHeight += view.frame.size.height;
}

[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];

扫码关注云+社区