开发者

iPhone ScrollView in Tab Bar Controller - ScrollView doesn't scroll. Can someone help?

开发者_Go百科I'm trying to add a ScrollView to a Tab Bar Controller. Problem is the ScrollView won't scroll, and I can't for the life of me find out why on Google. If I replace the ScrollView with a TextView, that one scrolls, and so does a TableView.

You can get the code demonstrating this from here: git://gitorious.org/scrollview-in-tabbar-project/scrollview-in-tabbar-project.git

Can anyone tell me what I'm doing wrong? Please notice that, annoyingly, the TextView that is inside the ScrollView scrolls. Not sure if this is related, but I don't get a viewWillAppear call when I select the ScrollView's tab.


You haven't set the contentSize of your scrollview. You are responsible to set the contentSize property. In your case, you can add this to viewDidLoad in your SecondViewController:

-(void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1024);
}

Set the height of the contentSize to what ever fits your needs (possibly you have to calculate the height to fit what you are putting in the scrollview).

Here's an example from the Apple documentation: Creating Scroll Views in Interface Builder


Based on Erik Tjernlund's answer, my code now looks like this:

- (void) viewDidLoad
{
    [super viewDidLoad];

    CGFloat maxDepth = 0;

    for (int i = 0; i < scrollView.subviews.count; i++)
    {
        UIView *aSubview = (UIView *) [scrollView.subviews objectAtIndex:i];
        CGFloat depth = aSubview.frame.origin.y + aSubview.frame.size.height;
        if (depth > maxDepth)
            maxDepth = depth;
    }
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, maxDepth);
}

and right now it looks like it satisfies my needs. It's also probably not 100% correct, and I'll find that out as I go deeper.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜