1
votes

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;

}
3
Could you get this working with Auto Layout?mvb

3 Answers

2
votes

You may have auto layout enabled on your storyboard.

You can disable auto layout by selecting the file inspector in the storyboard and unchecking "Use Autolayout".

0
votes

you need put this code in your .m

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

after

update your code for this

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (IS_WIDESCREEN) {

        self.meuScroll.frame = CGRectMake(0, 0, 320, 468);

    } else {

        self.meuScroll.frame = CGRectMake(0, 0, 320, 380);

    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (IS_WIDESCREEN) {

        self.meuScroll.contentSize = CGSizeMake(320, 468);

    } else {

        self.meuScroll.contentSize = CGSizeMake(320, 380);

    }
}
0
votes

The code in the question above is built upon the sample code for paging using UIScrollView provided by Apple. I used the same code for my application, and spent hours going through tutorials, stack overflow questions and sample codes, but nothing could fix the Auto Layout issue with its scrollview. Turns out you just need to add one line of code in the method that returns scrollView as the view for your controller. (This is the PhoneContentController class in the sample code)

- (UIView *)view
{
    [self.scrollView setTranslatesAutoresizingMaskIntoConstraints:YES];
    return self.scrollView;
}