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

问题描述 投票:1回答:5

我有一个UIView,它是UIScrollView的子视图。

我这样做是为了在viewDidLoad中设置内容大小(我在此网站上找到了有用的代码段:]

CGFloat scrollViewHeight = 0.0f;
for (UIView* view in self.scrollView.subviews)
{
    scrollViewHeight += view.frame.size.height;
}

[self.scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];

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

在IB中启用了滚动功能,所以我缺少什么?

iphone ios cocoa-touch uiscrollview
5个回答
5
投票

userInteractionEnabled属性等于TRUE吗?也许您的滚动视图有一个子视图正在窃取触摸信息?


22
投票

这也发生在我身上-似乎当从nib文件加载视图时,发生了一些调整大小的操作[[after 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); }


2
投票
我同意Sepehr。肯定有其他东西在偷您的手。您可以将UIScollViewDelegate添加到您的类中进行测试,并在下面添加事件回调方法之一。如果您在触摸滚动视图时没有得到呼叫,则说明Sepehr是正确的。

1
投票
问题来自基于约束的自动布局设置contentSize。由于我的视图很复杂,并且自动布局无法完成​​任务,因此我通过覆盖

setContentView:


0
投票
作为ddenis建议使用performSelector:withObject:afterDelay的替代方法,您可以在viewDidLayoutSubviews中调用setContentSize:
© www.soinside.com 2019 - 2024. All rights reserved.