UIScrollView will not scroll even when I set the content size

I have a UIView, which is a subpoint of UIScrollView.

I did this to set the content size in viewDidLoad (a useful snippet I found on this site):

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

Using NSLogs, I determined that the size of the content size is greater than the scroll height. But it still won’t scroll. (I'm only interested in vertical scrolling.)

Scrolling is included in IB, so what am I missing?

+1
source share
4 answers

property userInteractionEnabled equal to TRUE? Perhaps you have an add-on on your scroll list that stole the strokes?

+5
source

It also happened to me - it seems that when the view is loaded from the nib file, some resizing occurs after viewDidLoad and even after viewWillAppear. The call stack shows that it is being resized using OldSuperviewSize, which does this

 #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 

I have not yet found a suitable solution, besides setting contentSize later by calling the function performSelector: withObject: afterDelay

 -(void)viewDidLoad { [self performSelector:@selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1]; } -(void)adjustScrollViewContentSize { _scrollView.contentSize = CGSizeMake(4000, 4000); } 
+22
source

I agree with seper. Something else should steal your touch. You can test this purchase by adding a UIScollViewDelegate to your class and add one of the callback methods for the events below. If you don’t get called when you touch your scroll view, you know that Sepehr is right.

 (void)scrollViewDidScroll:(UIScrollView *)scrollView (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
+2
source

The problem is with the automatic configuration of contentSize based on restrictions. Since my view was complicated and automatic layout did not fit the task, I solved it by overriding setContentView: and ignoring auto layouts zero height setContentSize: message.

 @interface MyView : UIScrollView {} @end @implementation MyView - (void)setContentSize:(CGSize)aSize { if (aSize.height > 0) [super setContentSize:aSize]; } @end 
+1
source

All Articles