jontelang
jontelang

Reputation: 609

Multiple UIViewControllers in "one" view

I am making an app where I have multiple different screens that does different things. I have it all working with pop/push in an UINavigationController.

The layout is then something like this. The gray is each "uiviewcontroller", the rectangle is part of the background-picture and the RGB is the 'content' in each uiviewcontroller.

img1

I want to make it seem that all of the different screens are in the same "space". Like the next image.

I have been playing with custom segues and it "kind of" works. It removes the "push"/"pop" visual and it animates smoothly, it works great if the background is of one color. But that's not the case, in fact my background will have an animation where it spins ever so slowly.

img2

So how should I go about this?

Things I've tried or thought about

  1. Custom segues. Require a lot of fiddly animations, not sure if I should pursue this as it seems like a hack (eg I am now animating the background separately to make it look like it is stationary while the RGB (content) moves.)
  2. Transitions, I've just looked at this but if I am not mistaken it will have the same issues as above.
  3. Some way of adding 3 UIViewControllers into a UIScrollView (or similar) and somehow "change" the active UIViewController/delegate/something depending on which screen I am currently watching.
  4. Each RGB would be it's own UIView and managed by one big UIViewController.

I've been sitting on this problem for a good 2 days now but I am not sure how to best go about it.

EDIT: I used the UIPageViewController solution below and this is the resulting "app".

result

Upvotes: 1

Views: 632

Answers (3)

Ryan
Ryan

Reputation: 4884

Have you tried using UIPageViewController? If you want change the UIViewController with swipe gesture, it will be the answer. It' available from iOS 6.

RootVC.h

@interface RootVC : UIPageViewController
@end

RootVC.m

@interface RootVC () <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
{
    NSArray *_viewControllers;
}
@end

@implementation RootVC

- (void)viewDidLoad
{
    [super viewDidLoad];

    LeftViewController *lv = [[LeftViewController alloc] init];
    CenterViewController *cv = [[CenterViewController alloc] init];
    RightViewController *rv = [[RightViewController alloc] init];
    _viewControllers = @[lv, cv, rv];

    [self setViewControllers:@[cv] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {

}];
[self setDelegate:self];
[self setDataSource:self];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [_viewControllers indexOfObject:viewController];
    if(index > 0)
    {
        index--;
        return [_viewControllers objectAtIndex:index];
    }
    else
    {
        return nil;
    }
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [_viewControllers indexOfObject:viewController];
    if(index < 2)
    {
        index++;
        return [_viewControllers objectAtIndex:index];
    }
    else
    {
        return nil;
    }
}

Upvotes: 1

Jason McDermott
Jason McDermott

Reputation: 411

You don't need three UIViewControllers.

  1. add three UIView sub objects to your UIViewController's UIView, adjusting the frame.origin.x for each (so they are either on screen or not).
  2. Add a swipe gesture recogniser to your UIViewController for right+left,
  3. then when it's triggered, move the views on or off screen as needed. You can use the standard UIView AnimateWithDuration method, or a more custom CAAnimation.

Upvotes: 0

F1ank3r
F1ank3r

Reputation: 199

Have you tried putting your view controllers in container views? Maybe it will fit your needs.

https://developer.apple.com/videos/wwdc/2011/?id=102

Upvotes: 0

Related Questions