iphone之即使我设置了内容大小,UIScrollView 也不会滚动

findumars 阅读:47 2025-05-04 20:05:19 评论:0

我有一个 UIView,它是 UIScrollView 的 subview 。

我这样做是为了在 viewDidLoad 中设置内容大小(我在这个网站上找到的有用片段):

CGFloat scrollViewHeight = 0.0f; 
for (UIView* view in self.scrollView.subviews) 
{ 
    scrollViewHeight += view.frame.size.height; 
} 
 
[self.scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))]; 

使用 NSLogs 我确定内容大小高度大于 ScrollView 的高度。但是,它仍然不会滚动。 (我只对垂直滚动感兴趣。)

在 IB 中启用了滚动,所以我错过了什么?

请您参考如下方法:

这也发生在我身上 - 似乎当从 nib 文件加载 View 时,在 viewDidLoad 之后甚至在 viewWillAppear 之后都会发生一些调整大小。
调用堆栈显示正在执行此操作的是 resizeWithOldSuperviewSize

#0  0x0000f040 in -[ScrollView setContentSize:] at .../ScrollView.m:49 
#1  0x00092863 in -[UIScrollView _resizeWithOldSuperviewSize:] () 
#2  0x0007357a in -[UIView(Geometry) resizeWithOldSuperviewSize:] () 
#3  0x00072178 in __46-[UIView(Geometry) resizeSubviewsWithOldSize:]_block_invoke_0 () 
#4  0x01ccb5a7 in __NSArrayChunkIterate () 
#5  0x01ca303f in __NSArrayEnumerate () 
#6  0x01ca2a16 in -[NSArray enumerateObjectsWithOptions:usingBlock:] () 
#7  0x0007210f in -[UIView(Geometry) resizeSubviewsWithOldSize:] () 
#8  0x0056d14c in -[UIView(AdditionalLayoutSupport) _is_layout] () 
#9  0x00076dfe in -[UIView(Hierarchy) layoutSubviews] () 
#10 0x0007e92d in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] () 
#11 0x010fa6b0 in -[NSObject performSelector:withObject:] () 
#12 0x022a5fc0 in -[CALayer layoutSublayers] () 
#13 0x0229a33c in CA::Layer::layout_if_needed(CA::Transaction*) () 
#14 0x022a5eaf in -[CALayer layoutIfNeeded] () 
#15 0x0011d8cd in -[UIViewController window:setupWithInterfaceOrientation:] () 
#16 0x000661a6 in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] () 
#17 0x00064cbf in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] () 
#18 0x00064bd9 in -[UIWindow _setRotatableViewOrientation:duration:force:] () 
#19 0x00063e34 in __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke_0 () 
#20 0x00063c6e in -[UIWindow _updateToInterfaceOrientation:duration:force:] () 
#21 0x00064a29 in -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] () 
#22 0x00067922 in -[UIWindow setDelegate:] () 
#23 0x00111fec in -[UIViewController _tryBecomeRootViewControllerInWindow:] () 
#24 0x0005ebc4 in -[UIWindow addRootViewControllerViewIfPossible] () 
#25 0x0005edbf in -[UIWindow _setHidden:forced:] () 
#26 0x0005ef55 in -[UIWindow _orderFrontWithoutMakingKey] () 
#27 0x00067f67 in -[UIWindow makeKeyAndVisible] () 
#28 0x0000379a in -[AppDelegate application:didFinishLaunchingWithOptions:] at .../AppDelegate.m:24 

除了稍后通过​​调用 performSelector:withObject:afterDelay 设置 contentSize 之外,我还没有找到合适的解决方案
-(void)viewDidLoad 
{ 
    [self performSelector:@selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1]; 
} 
 
-(void)adjustScrollViewContentSize 
{ 
    _scrollView.contentSize = CGSizeMake(4000, 4000); 
} 


标签:程序员
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号