Search code examples
iphonedynamicpaginationuiscrollviewsubview

Create a dynamic uiscrollview subview


I was wondering if it was possible to dynamically create a subview (page) for my UIScrollView. I just need it to have 2 UILabel. One on the top and one on the bottom. If that is possible, could someone tell me how, or point me to a tutorial/video?

I used a tutorial and I got it going very good. But it only has 1 UILabel on the bottom. The link to that one is here http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

This is were I think most of the action is.

_objects = [[NSArray alloc] initWithObjects:@"",@"Does not include today", @"This does include today", @"Does not include today", @"This does include today", @"Includes the weekends!", nil];

_detailLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 320.0, 460.0)];
_detailLabel1.textAlignment = UITextAlignmentCenter;
_detailLabel1.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel1.textColor=[UIColor whiteColor];
_detailLabel1.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];


_detailLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(320.0, 0.0, 320.0, 460.0)];
_detailLabel2.textAlignment = UITextAlignmentCenter;
_detailLabel2.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel2.textColor=[UIColor whiteColor];
_detailLabel2.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];


_detailLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(640.0, 0.0, 320.0, 460.0)];
_detailLabel3.textAlignment = UITextAlignmentCenter;
_detailLabel3.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel3.textColor=[UIColor whiteColor];
_detailLabel3.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
// We are going to show all the contents of the _objects array
// using only these three UILabel instances, making them jump 
// right and left, replacing them as required:

[scrollView addSubview:_detailLabel1];
[scrollView addSubview:_detailLabel2];
[scrollView addSubview:_detailLabel3];

I have other code in other places, but this is where I think the main part of it is.


Solution

  • Instead of adding labels directly to scrollview. Create a subview then add the labels to subview then add to scrollview. So your work done.

    piece of code took from tutorials you refered.

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
        for (int i = 0; i < colors.count; i++) {
            CGRect frame;
            frame.origin.x = self.scrollView.frame.size.width * i;
            frame.origin.y = 0;
            frame.size = self.scrollView.frame.size;
    
            UIView *subview = [[UIView alloc] initWithFrame:frame];
            subview.backgroundColor = [colors objectAtIndex:i];
            [self.scrollView addSubview:subview];
    
    _detailLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 100.0, 40.0)];
    _detailLabel1.textAlignment = UITextAlignmentCenter;
    _detailLabel1.font = [UIFont boldSystemFontOfSize:15.0];
    _detailLabel1.textColor=[UIColor whiteColor];
    _detailLabel1.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
     [subview addSubview:_detailLabel1];
    
    _detailLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 300.0, 100.0, 40.0)];
    _detailLabel2.textAlignment = UITextAlignmentCenter;
    _detailLabel2.font = [UIFont boldSystemFontOfSize:15.0];
    _detailLabel2.textColor=[UIColor whiteColor];
    _detailLabel2.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
    [subview addSubview:_detailLabel2];
    
    
            [subview release];
        }