iphone - 如何自动调整 UIScrollView 的大小以适合其内容

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

类似:

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

最佳答案

我遇到过的根据包含的 subview 更新 UIScrollView 内容大小的最佳方法:

Objective-C

CGRect contentRect = CGRectZero;

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

Swift

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

https://stackoverflow.com/questions/2944294/

相关文章:

objective-c - 使用 ARC 时是否在 dealloc 中将属性设置为 nil?

ios - Xcode/模拟器 : How to run older iOS version?

objective-c - RootViewController 切换过渡动画

iphone - 如何获取 UIImage 并给它一个黑色边框?

iphone - iPhone 上 NSString 的 AES 加密

ios - 将参数传递给 addTarget :action:forControlEvents

objective-c - 将空格序列折叠成单个字符并修剪字符串

iphone - 如何将图像保存到相机胶卷?

iphone - 使用 Cocoa 和 Objective-C 理解引用计数

ios - 以编程方式触发按钮单击事件?