本文介绍了如何自动调整 UIScrollView 的大小以适合其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让 UIScrollView
自动调整到它滚动的内容的高度(或宽度)?
类似于:
[scrollView setContentSize:(CGSizeMake(320, content.height))];
解决方案
我见过的最好的方法是根据其包含的子视图更新 UIScrollView
的内容大小:
目标 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 inrect = rect.union(view.frame)}scrollView.contentSize = contentRect.size
Is there a way to make a UIScrollView
auto-adjust to the height (or width) of the content it's scrolling?
Something like:
[scrollView setContentSize:(CGSizeMake(320, content.height))];
解决方案
The best method I've ever come across to update the content size of a UIScrollView
based on its contained subviews:
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 的大小以适合其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!