iospaginationuiscrollviewpadding

Setting rootViewController on UIWindow changes UIScrollView with paging layout


UPDATE

It turns out that the code below is not actually the problem. In my app delegate I am doing:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] init];

    self.window.rootViewController = self.viewController;// <-- this does not work
    //[self.window addSubview:self.viewController.view]; // <-- this works

    [self.window makeKeyAndVisible];
    return YES;
}

If I remove the statement "self.window.rootViewController = self.viewController" and just add the viewController's view to the window, it works. Can anyone explain this? Does setting the rootViewController on the window constrain the child's bounds? I have tried to go through the docs, but it doesn't mention much about this.

ORIGINAL POST

I am having trouble adding padding to pages in a UIScrollView. I am basically trying to setup a simple scroll view that shows UIViews in different pages separated by a predefined padding (kind of like the Photos app without photos). I have been trying to follow Apple's ScrollView example from WWDC 2010 and their sample app PhotoScroller but always come up with padding showing in the view. The app currently hides the status bar and adds 1 view controller to the window. To make things simple, each of the pages should show a UIView that is colored green, while the space where there is padding is yellow. You should only see the yellow when the user is scrolling. Here are the first 3 pages:

pages 1, 2, and 3

I have a single class level field called pagingScrollView declared in the .h file. In my single view controller, I am basically just trying to follow what the sample code is doing.

#define PADDING  10
#define PAGE_COUNT 3

- (void)loadView
{
    CGRect pagingScrollFrame = [[UIScreen mainScreen] bounds];
    pagingScrollFrame.origin.x -= PADDING;
    pagingScrollFrame.size.width += (2 * PADDING);
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollFrame];

    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor yellowColor];
    pagingScrollView.contentSize = CGSizeMake(pagingScrollFrame.size.width * PAGE_COUNT, pagingScrollFrame.size.height);

    self.view = pagingScrollView;

    for(int i = 0; i < PAGE_COUNT; i++) {
        CGRect frame = [self frameForPageAtIndex:i];
        UIView *page = [[UIView alloc] initWithFrame:frame];
        page.backgroundColor = [UIColor greenColor];
        [pagingScrollView addSubview:page];
    }
}

- (CGRect)frameForPageAtIndex:(NSUInteger)index {
    CGRect bounds = pagingScrollView.bounds;
    CGRect pageFrame = bounds;

    pageFrame.size.width -= (2 * PADDING);
    pageFrame.origin.x = (bounds.size.width * index) + PADDING;

    return pageFrame;
}

The pagingScrollFrame has a width of 340, so (I thought) that scroll view would be broken up into pages of 340 pixels. What am I missing?


Solution

  • The reason the paging is off is because setting the RootViewController on the window is apparently doing something behind the scenes (what that is, I don't know). To fix is, I use the old way of adding a view to the window.

    [self.window addSubview:self.viewController.view];
    

    If you think you know how to fix it while setting the RootViewController, please let me know!