iosobjective-cxcodepaginationuiscrollview

Incorrect Page size in UIScrollview Paging


I am trying to make a simple Paging with UIScrollview, But pages are getting out after the second screen. Here is my code.

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

    NSArray *colors =[NSArray arrayWithObjects:@"1. This is my first Page ",@"2. This is my Second Page",@"3. This is my third Page",@"4. This is my fourth Page",@"5. This is my fifth Page",nil];

for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = (scroll_events.frame.size.width) * i;
    frame.origin.y = 0;
    frame.size = scroll_events.frame.size;
    
    UIView *subview = [[UIView alloc] initWithFrame:frame];
    [self.scroll_events addSubview:subview];
    
        UITextView *myTextView = [[UITextView alloc] init];
        myTextView.text = colors[i];
        [subview addSubview:myTextView];
        [myTextView setBackgroundColor: [UIColor redColor]];
        [myTextView setTextColor: [UIColor blackColor]];
        [myTextView setFont: [UIFont fontWithName:@"Lobster 1.4" size:30]];
        myTextView.frame = CGRectMake(0.0,0.0,scroll_events.bounds.size.width,scroll_events.bounds.size.height);
        myTextView.editable = NO;
        myTextView.selectable = NO;
        myTextView.scrollEnabled = NO;
}
self.scroll_events.contentSize = CGSizeMake(self.scroll_events.frame.size.width * colors.count, self.scroll_events.frame.size.height);
}

Scroll View at second Page Here is the screen shot of my Scroll View


Solution

  • In ViewDidLoad maybe the frame of your scroll view was still not set.

    Try that code in ViewDidAppear and check if it works.

    Advice: You should subclass that UISCrollView and initialize the view there, not in the ViewController.

    Let me know it that worked!

    Regards.