如何自动调整UIScrollView的大小以适合内容 [英] How do I auto size a UIScrollView to fit the content

查看:558
本文介绍了如何自动调整UIScrollView的大小以适合内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to make a UIScrollView auto-adjust to the height (or width) of the content it's scrolling?

类似于:

[scrollView setContentSize:(CGSizeMake(320, content.height))];


推荐答案

我遇到的最好的更新方法基于其包含的子视图 UIScrollView 的内容大小:

The best method I've ever come across to update the content size of a UIScrollView based on its contained subviews:

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的大小以适合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆