Search code examples
iosobjective-cxcodeuiscrollviewswipe

Scrolling between view controllers Xcode


im trying to create an App where you have 3 view controllers and you can swipe between them using a scroll view.

I managed to have 2 views working perfectly but when I tried to add a third it just replaced the second.

Here is the code:

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    ChatsViewController *aViewController = [[ChatsViewController alloc]init];
    [self addChildViewController:aViewController];
    [self.scrollView addSubview:aViewController.view];
    [aViewController didMoveToParentViewController:self];

    FriendsViewController *bViewController = [[FriendsViewController alloc]init];
    CGRect frame = bViewController.view.frame;
    frame.origin.x = 320;
    bViewController.view.frame = frame;

    ProfileView *cViewController = [[ProfileView alloc]init];
    CGRect frame2 = cViewController.view.frame;
    frame2.origin.x = 640;
    cViewController.view.frame = frame;

    [self addChildViewController:bViewController];
    [self.scrollView addSubview:bViewController.view];
    [bViewController didMoveToParentViewController:self];

    [self addChildViewController:cViewController];
    [self.scrollView addSubview:cViewController.view];
    [cViewController didMoveToParentViewController:self];

    self.scrollView.contentSize = CGSizeMake(960, self.view.frame.size.height);
    self.scrollView.pagingEnabled = YES;

    }  

I'm using this library: https://github.com/gneil90/CustomContainerViewController

Thank you for your help!


Solution

  • You have

    cViewController.view.frame = frame
    

    should be

    cViewController.view.frame = frame2