UIPageViewController keeping previous ViewController on the Background of the view

别说谁变了你拦得住时间么 提交于 2020-01-25 00:40:14

问题


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:


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.



来源:https://stackoverflow.com/questions/28784968/uipageviewcontroller-keeping-previous-viewcontroller-on-the-background-of-the-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!