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

查看:89
本文介绍了如何自动调整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:

Objective-C

CGRect contentRect = CGRectZero;

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

快速

let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
    rect = rect.union(view.frame)
}
scrollView.contentSize = contentRect.size

这篇关于如何自动调整UIScrollView的大小以适合其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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