Setting the content size of my UIScrollView in viewDidLoad/viewWillAppear has no effect - why?

て烟熏妆下的殇ゞ 提交于 2019-12-21 20:06:17

问题


I'm trying to force the content size of my UIScrollVIew when it's container loads. I'm finding this much more difficult than I'd imagine it would be. I've tried setting the content size in both viewDidLoad and viewWillAppear, like so:

-(void)viewWillAppear:(BOOL)animated
{    
    CGSize newContentSize = CGSizeMake(scrollView.contentSize.width, scrollView.frame.size.height + 80.0);
    [scrollView setContentSize:newContentSize];    
}

If I place a breakpoint right after I call setContentSize, I can see that the new content size is indeed what I set it to.

However, if I then check the content size at some arbitrary point after viewWillAppear, such as, say, scrollViewDidScroll, the content size has reverted back to the size of the "actual" content in the scroll view, and not the size I tried forcing it to be in viewDidAppear.

Why is this?


回答1:


I have no idea what is resetting it on you, but I can help you find the answer.

What I usually do in a situation like this is subclass UIScrollView (usually as "XXXUIScrollView"), override setContentSize: (to just call [super setContentSize:size], maybe with an NSLog), and set a breakpoint in the overridden method to see what is setting the value.




回答2:


I got exactly the same problem and the only workaround I found so far was to do performSelector:withObject:afterDelay - to set the contentSize property after some small delay: UIScrollView won't scroll even when I set content size



来源:https://stackoverflow.com/questions/7101656/setting-the-content-size-of-my-uiscrollview-in-viewdidload-viewwillappear-has-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!