I had made a previous project that used UIScrollView with Paging, but with this new project, I have been banging my head over this and can't get it working. I think the problem may possibly lay with AutoLayout. At first I couldn't get the paging motion to even work until I realized that all the initialization has to be done in viewDidAppear instead of ViewDidLoad.
Now that I have the action of paging done, the subView don't load! It will just push the current view I setup in Storyboard out of the way and just shows the blank background.
Note that the code here is almost identical to the my previous project, yet I can't get the 2nd view (page 2) to show up.
Here is some of the code:
- (void)viewDidAppear:(BOOL)animated{
self.scrollView.delegate = self;
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, (scrollView.frame.size.height - 50));
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page
{
if (page <= 0)
return;
if (page >= kNumberOfPages)
return;
PageOneViewController *controller = [viewControllers objectAtIndex:1];
if ((NSNull *)controller == [NSNull null])
{
if(page == 1)
{
controller = [[PageOneViewController alloc] initWithRequestNumber: @"100" forID:self.idNumberFromLogin];
[viewControllers replaceObjectAtIndex:page withObject:controller];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
}
In the PageOneViewController
- (id)initWithRequestNumber:(NSString *)requestID forID: (NSString *) employeeID
{
if (self = [super initWithNibName:@"OpenRequestCommView" bundle:nil])
{
}
return self;
}