类似这样的功能:
[scrollView setContentSize:(CGSizeMake(320, content.height))];
[scrollView setContentSize:(CGSizeMake(320, content.height))];
我发现更新基于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
UIScrollView无法自动获取其内容的高度。必须自己计算高度和宽度。
可以使用以下方式进行计算
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in scrollView.subviews)
{
scrollViewHeight += view.frame.size.height;
}
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
但是这仅适用于视图一个接一个地排列的情况。如果您的视图并排排列,如果您不想将滚动区域的内容设置得比实际更大,则只需添加其中一个视图的高度即可。
int y = CGRectGetMaxY(((UIView*)[_scrollView.subviews lastObject]).frame); [_scrollView setContentSize:(CGSizeMake(CGRectGetWidth(_scrollView.frame), y))];
这将使滚动视图的内容尺寸自适应其子视图的大小。 - Gal在所有相关视图上将translatesAutoresizingMaskIntoConstraints
设置为NO
。
使用与滚动视图外部的约束来定位和调整滚动视图的大小。
使用约束来布局滚动视图内的子视图,确保约束与滚动视图的四个边缘相连,并且不依赖于滚动视图来确定它们的大小。
我添加了这段代码到Espuz和JCC的答案中。它使用子视图的y位置,不包括滚动条。 编辑 使用可见的最低子视图的底部。
+ (CGFloat) bottomOfLowestContent:(UIView*) view
{
CGFloat lowestPoint = 0.0;
BOOL restoreHorizontal = NO;
BOOL restoreVertical = NO;
if ([view respondsToSelector:@selector(setShowsHorizontalScrollIndicator:)] && [view respondsToSelector:@selector(setShowsVerticalScrollIndicator:)])
{
if ([(UIScrollView*)view showsHorizontalScrollIndicator])
{
restoreHorizontal = YES;
[(UIScrollView*)view setShowsHorizontalScrollIndicator:NO];
}
if ([(UIScrollView*)view showsVerticalScrollIndicator])
{
restoreVertical = YES;
[(UIScrollView*)view setShowsVerticalScrollIndicator:NO];
}
}
for (UIView *subView in view.subviews)
{
if (!subView.hidden)
{
CGFloat maxY = CGRectGetMaxY(subView.frame);
if (maxY > lowestPoint)
{
lowestPoint = maxY;
}
}
}
if ([view respondsToSelector:@selector(setShowsHorizontalScrollIndicator:)] && [view respondsToSelector:@selector(setShowsVerticalScrollIndicator:)])
{
if (restoreHorizontal)
{
[(UIScrollView*)view setShowsHorizontalScrollIndicator:YES];
}
if (restoreVertical)
{
[(UIScrollView*)view setShowsVerticalScrollIndicator:YES];
}
}
return lowestPoint;
}
这是在 Swift 中接受的答案,适用于那些懒得转换的人 :)
var contentRect = CGRectZero
for view in self.scrollView.subviews {
contentRect = CGRectUnion(contentRect, view.frame)
}
self.scrollView.contentSize = contentRect.size
或者更懒一点
let contentRect = self.scrollView.subviews.reduce(CGRect.zero) { CGRect.union($0, $1.frame) }
self.scrollView.contentSize = contentRect.size
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
- drew..以下扩展将对Swift有所帮助。
extension UIScrollView{
func setContentViewSize(offset:CGFloat = 0.0) {
// dont show scroll indicators
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
var maxHeight : CGFloat = 0
for view in subviews {
if view.isHidden {
continue
}
let newHeight = view.frame.origin.y + view.frame.height
if newHeight > maxHeight {
maxHeight = newHeight
}
}
// set content size
contentSize = CGSize(width: contentSize.width, height: maxHeight + offset)
// show scroll indicators
showsHorizontalScrollIndicator = true
showsVerticalScrollIndicator = true
}
}
逻辑与提供的答案相同。但是,它省略了UIScrollView中隐藏视图,并在滚动指示器设置隐藏后执行计算。
此外,还有一个可选的函数参数,您可以通过将参数传递给函数来添加偏移值。
import UIKit
extension UIScrollView {
func resizeScrollViewContentSize() {
var contentRect = CGRect.zero
for view in self.subviews {
contentRect = contentRect.union(view.frame)
}
self.contentSize = contentRect.size
}
}
使用说明
scrollView.resizeScrollViewContentSize()
@leviathan提供的方案非常好,是使用FP(函数式编程)方法转换为Swift。
self.scrollView.contentSize = self.scrollView.subviews.reduce(CGRect(), {
CGRectUnion($0, $1.frame)
}.size
self.contentSize = self.subviews.reduce(CGRect(), { $0.union($1.frame) }).size
- John Rogers您可以通过计算哪个子视图“到达最远”来获取UIScrollView内部内容的高度。要计算这个值,您需要考虑起始位置Y和项目高度。
float maxHeight = 0;
for (UIView *child in scrollView.subviews) {
float childHeight = child.frame.origin.y + child.frame.size.height;
//if child spans more than current maxHeight then make it a new maxHeight
if (childHeight > maxHeight)
maxHeight = childHeight;
}
//set content size
[scrollView setContentSize:(CGSizeMake(320, maxHeight))];
通过这种方式,项目(子视图)不必直接堆叠在彼此下面。
int y = CGRectGetMaxY(((UIView*)[_scrollView.subviews lastObject]).frame); [_scrollView setContentSize:(CGSizeMake(CGRectGetWidth(_scrollView.frame), y))];
这个解决方案是我在这个页面上添加的评论。在得到19个赞同之后,我决定将这个解决方案作为正式答案添加,以造福社区!
viewDidLayoutSubviews
中运行这段代码,以便自动布局完成。在iOS7中,它可以正常工作,但是在测试iOS6时,由于某种原因自动布局没有完成,导致我得到了错误的高度值。所以我改用了viewDidAppear
,现在代码可以正常工作了。这只是提醒,也许有人会需要这个信息。谢谢。 - Hatem Alimam