开发者

UIScrollView won't scroll even when I set content size

I have a UIView that is a subview of UIScrollView.

I've done this to set the content size in viewDidLoad (useful snippet I found on this site):

CGFloat scrollViewHeight = 0.0f;
for (UIView* view in self.scrollView.subviews)
{
    scrollViewHeight += view.frame.开发者_StackOverflow社区size.height;
}

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

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

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


This also happened to me - seems like when view is loaded from nib file some resizing happens after viewDidLoad and even after viewWillAppear. The call stack shows it is resizeWithOldSuperviewSize who is doing that

#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 didn't find a proper solution yet except setting contentSize later by calling performSelector:withObject:afterDelay

-(void)viewDidLoad
{
    [self performSelector:@selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1];
}

-(void)adjustScrollViewContentSize
{
    _scrollView.contentSize = CGSizeMake(4000, 4000);
}


is userInteractionEnabled property equal to TRUE? Perhaps you have a subview over your scrollview that is stealing the touches?


I agree with Sepehr. Something else must be stealing your touches. You can test this buy adding UIScollViewDelegate to your class and add one of the event call back methods below. If when you touch your scroll view they dont get called then you know Sepehr is correct.

(void)scrollViewDidScroll:(UIScrollView *)scrollView
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView


The issue comes from auto layout setting the contentSize based on constraints. Since my view was complex and auto layout was not up to 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


As an alternative to the use of performSelector:withObject:afterDelay suggested by ddenis, you could call setContentSize in viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //do stuff here to calculate the content width and height
    // ...
    //then set the content size accordingly
    [self.scrollView setContentSize:(CGSizeMake(contentWidth, contentHeight))];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜