jwe*_*rre 126 iphone cocoa-touch objective-c uiscrollview ios
有没有办法UIScrollView
自动调整滚动内容的高度(或宽度)?
就像是:
[scrollView setContentSize:(CGSizeMake(320, content.height))];
Run Code Online (Sandbox Code Playgroud)
lev*_*han 301
我遇到的最好的方法是更新UIScrollView
基于其包含的子视图的内容大小:
Objective-C的
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
Run Code Online (Sandbox Code Playgroud)
迅速
let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = contentRect.size
Run Code Online (Sandbox Code Playgroud)
eme*_*gro 75
UIScrollView不会自动知道其内容的高度.您必须自己计算高度和宽度
用类似的东西做
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in scrollView.subviews)
{
scrollViewHeight += view.frame.size.height;
}
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
Run Code Online (Sandbox Code Playgroud)
但这只有在视图低于另一个视图时才有效.如果您有一个彼此相邻的视图,如果您不想将滚动条的内容设置为大于实际值,则只需添加一个视图的高度.
Ada*_*ite 40
设置translatesAutoresizingMaskIntoConstraints
为NO
涉及的所有视图.
使用滚动视图外部的约束来定位和调整滚动视图的大小.
使用约束在滚动视图中布置子视图,确保约束绑定到滚动视图的所有四个边缘,并且不依赖滚动视图来获取它们的大小.
资料来源:https: //developer.apple.com/library/ios/technotes/tn2154/_index.html
ric*_*chy 35
我把它添加到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;
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*osh 13
对于懒得转换它的人来说,这是swift中接受的答案:)
var contentRect = CGRectZero
for view in self.scrollView.subviews {
contentRect = CGRectUnion(contentRect, view.frame)
}
self.scrollView.contentSize = contentRect.size
Run Code Online (Sandbox Code Playgroud)
这是@ leviatan答案的Swift 3改编:
延期
import UIKit
extension UIScrollView {
func resizeScrollViewContentSize() {
var contentRect = CGRect.zero
for view in self.subviews {
contentRect = contentRect.union(view.frame)
}
self.contentSize = contentRect.size
}
}
Run Code Online (Sandbox Code Playgroud)
用法
scrollView.resizeScrollViewContentSize()
Run Code Online (Sandbox Code Playgroud)
非常好用!
扩展后将有助于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
}
}
Run Code Online (Sandbox Code Playgroud)
逻辑与给定答案相同.但是,它忽略了隐藏的视图,UIScrollView
并在滚动指示器设置隐藏后执行计算.
此外,还有一个可选的函数参数,您可以通过将参数传递给函数来添加偏移值.
小智 5
来自@leviathan的优秀和最佳解决方案.只需使用FP(函数式编程)方法转换为swift.
self.scrollView.contentSize = self.scrollView.subviews.reduce(CGRect(), {
CGRectUnion($0, $1.frame)
}.size
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
155458 次 |
最近记录: |