cwRichardKim / RKSwipeBetweenViewControllers

Swipe between ViewControllers like in the Spotify or Twitter app with an interactive Segmented Control in the Navigation Bar
MIT License
1.67k stars 156 forks source link

viewWillAppear causes controller to initialize again #27

Open D-32 opened 9 years ago

D-32 commented 9 years ago

The setup code inside viewWillAppear should only be performed once. Inside my app it happens that I push a view controller and when navigating back viewWillAppear is called again. This then causes all the pages to be setup again and the current index is reset.

nazywamsiepawel commented 7 years ago

hey there,

I have been facing the same issue and came up with a solution of quality proportional to the amount of time I had to investigate the issue. tl;dr - lame but works.

You need to change that :

-(void)viewWillAppear:(BOOL)animated {
    [self setupPageViewController];
    [self setupSegmentButtons];

    CGRect sizeRect = [UIScreen mainScreen].applicationFrame;
    float width = sizeRect.size.width;
    NSInteger xCoor = (width/[viewControllerArray count])*self.currentPageIndex-X_OFFSET;
    selectionBar.frame = CGRectMake(xCoor, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}

And then update the first view controller that is being loaded currentPageIndex.

-(void)setupPageViewController {
    pageController = (UIPageViewController*)self.topViewController;
    pageController.delegate = self;
    pageController.dataSource = self;
    [pageController setViewControllers:@[[viewControllerArray objectAtIndex:self.currentPageIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self syncScrollView];
}

Not sure if it was a bug or by design but in my case pushing anything on top of the navigation and then going back was causing some rather unintuitive behaviour.