Mike S
Mike S

Reputation: 4122

UIView setBounds UIScrollView hell!

I have a bunch of 'rowviews' that I want to put in a vertical scroll view. I have created this rowView view as a separate nib in IB. They are sized at 1024/200 (ipad). Now I want to put them one by one in my parent UIScrollView. I tried a simple [vScroll addSubview:rowView] but this puts them overtop of eachother (I made the rowview transparent to check this). So then I started fooling with the bounds of each rowview to no avail. This is my code. Note 'self.yExtentSoFar' is initialised to 0. Imagine the code below called for each row:

            MyRowView *rowView = [[MyRowView alloc] init];

    float calculatedWidth = 0;
    // minus nav bar
    float calculatedHeight = 0;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIInterfaceOrientationPortrait){
        // iPad
        calculatedWidth =  768.0;
        calculatedHeight = 960.0;
    }else{
        // iPad
        calculatedWidth =  1024.0;
        calculatedHeight = 704.0;
    }

    [self.vScroll addSubview: rowView.view];
    [rowView.view setBounds:CGRectMake(0, self.yExtentSoFar, calculatedWidth,200)];
    [self.vScroll setContentSize:CGSizeMake(calculatedWidth, yExtentSoFar+200)];
    self.yExtentSoFar += 200;

So before I tried settings bounds the rowviews appeared overtop of each other. Understandable I guess. When I set the bounds, the 2nd row view hasn't appeared under the 1st as expected, instead I have to pull down the vScroll and the 2nd has appeared ABOVE the first off screen!

Could someone point to where I'm going wrong? Thanks a lot,

Mike

Upvotes: 1

Views: 4095

Answers (2)

ohhorob
ohhorob

Reputation: 11805

You want to layout your subviews by setting their frame.

Specifically you're confusing the reference co-ordinates.. bounds refers to how much of that view to show. Whereas the frame is where (& what size) should the view be placed in it's superview.

See "The Relationship of the Frame, Bounds, and Center" View Programming Guide for iPhone

Upvotes: 3

Matt Long
Matt Long

Reputation: 24466

You're doing it wrong ;-) What you have explained here is more or less a re-implementation of what you get using a UITableView. Use a UITableView and a custom table view cell. It will make your life much easier.

Upvotes: 2

Related Questions