views:

486

answers:

3

Hey guys,

I am having issues trying to get the pageControl sample code to work with rotation. I managed to get it to rotate but it does not visually load correctly until I start to scroll (then it works fine). Any Idea on how I can fix this problem? Here is a link to the project if you want to see it in action.

This code is based off the PageControl example apple has provided.

here is the code:

#import "ScrollingViewController.h"
#import "MyViewController.h"



@interface ScrollingViewController (PrivateMethods)

- (void)loadScrollViewWithPage:(int)page;

@end

@implementation ScrollingViewController
@synthesize scrollView;
@synthesize viewControllers;


- (void)viewDidLoad 
{
 amount = 5;
    [super viewDidLoad];
 [self setupPage];

}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload 
{
 [scrollView release];
}


- (void)dealloc 
{
    [super dealloc];
}

- (void)setupPage
{

    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < amount; i++) {
        [controllers addObject:[NSNull null]];
    }

    self.viewControllers = controllers;
    [controllers release];

    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * amount, 200);

    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];

}

#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }

 /*
  * We switch page at 50% across
  */
    CGFloat pageWidth = _scrollView.frame.size.width;


    int dog = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
   // pageControl.currentPage = page;

 [self loadScrollViewWithPage:dog - 1];
    [self loadScrollViewWithPage:dog];
    [self loadScrollViewWithPage:dog + 1];


}


- (void)loadScrollViewWithPage:(int)page {

    if (page < 0) return;
 if (page >= amount) return;

    MyViewController *controller = [viewControllers objectAtIndex:page];

    if ((NSNull *)controller == [NSNull null]) {
        controller = [[MyViewController alloc] initWithPageNumber:page];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }

    if (nil == controller.view.superview) {
        CGRect frame = scrollView.frame;

        frame.origin.x = frame.size.width * page;

        frame.origin.y = 0;
  controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
 [self setupPage];


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}


#pragma mark -
#pragma mark PageControl stuff


@end
A: 

Sounds like it's missing a -setNeedsDisplay: message.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{
 [self setupPage];

 // try this:
 [scrollView setNeedsDisplay:YES]; 
}
NSResponder
Hmm, sounds good but I am getting 'UIScrollView' may not respond to '-setNeedsDisplay' when I try that.. Maybe I need to do something differently?
leachianus.gecko
oh neat, [scrollView setNeedsDisplay]; seems to work, but it is still a little bit off.. for example when it gets to the last page in landscape mode I can still see the portrait cards off screen.. but otherwise it was a good step!
leachianus.gecko
A: 

It seems that when I rotate the UIScrollview it caches the previous pages and displays them in the background, how can I reset the scrollview when it rotates so it displays it correct?

thanks in advance!

leachianus.gecko
A: 

I'm having the same problem rotating a UIScrollview. I took the pagecontrol example from Apple and added subviews to it. When I rotate, only the first subview added is rotated, the other views don't

isaac