Search code examples
objective-cviewsubviewaddsubview

add multiple views in scrollview


I have a ScrollView and it will be filled with lots of views.

My views have a background and will receive a dynamic amount of buttons.

I need to add these views on my ScrollView.

Each view will be placed below the previous. My views have 1024 x 197

Hoe do I do that?

Thanks in advance for any help.

MORE DETAILS

I have created the view (the one I need repeated) on Interface Builder

@interface PortalBookViewController : UIViewController {
    UIView *prateleira;
    UIScrollView *ScrollView;
}

@property (nonatomic, retain) IBOutlet UIScrollView *ScrollView;
@property (nonatomic, retain) IBOutlet UIView *prateleira;

@end

I have linked that View(Interface Builder) with *prateleira

- (void)viewDidLoad {
    [super viewDidLoad];


    for (int i = 0; i < 4; i++) {
        switch (i) {
            case 0:
                prateleira = [prateleira initWithFrame:CGRectMake(0, 0, 1024, 197)];
                [ScrollView addSubview:prateleira];
                [prateleira release];
                break;
            case 1:
                prateleira = [prateleira initWithFrame:CGRectMake(0, 198, 1024, 197)];
                [ScrollView addSubview:prateleira];
                [prateleira release];
                break;
            case 2:
                prateleira = [prateleira initWithFrame:CGRectMake(0, 384, 1024, 197)];
                [ScrollView addSubview:prateleira];
                [prateleira release];
                break;
                break;
            case 3:
                prateleira = [prateleira initWithFrame:CGRectMake(0, 582, 1024, 197)];
                [ScrollView addSubview:prateleira];
                [prateleira release];
                break;
                break;
            default:
                break;
        }
    }

    [ScrollView release];


}

MODE DETAILS

If I do that: [self.ScrollView addSubview:prateleira]; it will show me this:

enter image description here

I think the problem is on setting the view position.


Solution

  • the solution was to create a uiview object like:

    UIView * shelf_view (NSInteger x, NSInteger y) {
    
        UIView *shelf = [[UIView alloc] initWithFrame:CGRectMake(x, y, 1024, 197)];
        UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shelf.png"]];
        [shelf addSubview:background];
        return shelf;
    
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    
        for (int i = 0; i < 4; i++) {
            switch (i) {
                case 0:
                    [ScrollView addSubview: shelf_view(0, 0)];
                    break;
                case 1:
                    [ScrollView addSubview: shelf_view(0, 197)];
                    break;
                case 2:
                    [ScrollView addSubview: shelf_view(0, 512)];
                    break;
                case 3:
                    [ScrollView addSubview: shelf_view(0, 777)];
                    break;
                default:
                    break;
            }
        }
        [super viewDidLoad];
        [ScrollView release];
    
    
    }
    

    It works perfectly.