UIPageViewController keeping previous ViewControll

2019-08-08 15:52发布

I have an UIPageViewController, that i bring up programatically, i have overwritten the viewControllerBeforeViewController and the viewControllerAfterViewController methods to dinamically create the viewcontrollers after each flip. The problem is that when i don't complete the flip page movement and the previous view controller comes back to the view, the same view controller remains on the background of the view, messing with the content i am displaying. I have tried using the callback method of the UIPageViewController delegate pageViewController didFinishAnimating and inside this method, i get the messages i intend to get, so when an animation is not completed, i can react to it, but when i refresh the UIPageViewController by setting its current view controller, the problem persists, here is the code:

  (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray *)previousViewControllers
       transitionCompleted:(BOOL)completed {
  if (!completed)
  {
    [self viewDidAppear:NO];
    for (UIViewController* vc in previousViewControllers) {
        [self setViewControllers: @[vc]
                       direction: UIPageViewControllerNavigationDirectionForward
                        animated: NO
                      completion: nil];
    }
    return;
   }

  }

The question is: How can i "clean" or "refresh" or "clear" the content of my UIPageViewController to remove the residual view and only show the one that is intended.

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-08 16:37

I had/have a similar problem. I use a UIPageViewController that can load different PageContentView's depending on the account (which is switchable). I was using the following code to remove the existing PageContent which worked to some degree:

[[self.pageViewController.childViewControllers objectAtIndex:0] willMoveToParentViewController:nil];
[[self.pageViewController.childViewControllers objectAtIndex:0].view removeFromSuperview];
[[self.pageViewController.childViewControllers objectAtIndex:0] removeFromParentViewController];

But when I swiped to a screen that wasn't the first screen, and then changed the UIPageViewController/PageContentView I could see the existing content behind the new content.

I looked into it closer and I saw that in the subviews of my UIViewController I had a an object called _UIPageViewControllerContentView.

So I wrote this bit of code to remove it and it seems to have helped.

for (id obj in self.view.subviews) {
    if ([[NSString stringWithFormat:@"%@", [obj class] ] isEqualToString:@"_UIPageViewControllerContentView"]) {
        [obj removeFromSuperview];
    }
}

Hope this helps.

查看更多
登录 后发表回答