UIPageViewController 将先前的 ViewController 保留在视图的背景上 [英] UIPageViewController keeping previous ViewController on the Background of the view
问题描述
我有一个 UIPageViewController,我以编程方式调出,我覆盖了 viewControllerBeforeViewController 和 viewControllerAfterViewController 方法,以便在每次翻转后动态创建视图控制器.问题是当我没有完成翻页移动并且前一个视图控制器返回到视图时,同一个视图控制器仍然在视图的背景上,与我正在显示的内容混淆.我已经尝试使用 UIPageViewController 委托 pageViewController didFinishAnimating 的回调方法,在这个方法中,我得到了我想要得到的消息,所以当动画没有完成时,我可以对其做出反应,但是当我通过设置其当前视图控制器刷新 UIPageViewController 时,问题仍然存在,这是代码:
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;
}
}
问题是:我如何清理"或刷新"或清除"我的 UIPageViewController 的内容以删除残留视图并仅显示预期的视图.
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.
推荐答案
我曾经/遇到过类似的问题.我使用 UIPageViewController 可以根据帐户(可切换)加载不同的 PageContentView.我使用以下代码删除了在某种程度上起作用的现有 PageContent:
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];
但是当我滑动到不是第一个屏幕的屏幕,然后更改 UIPageViewController/PageContentView 时,我可以看到新内容后面的现有内容.
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.
我仔细观察了一下,发现在我的 UIViewController 的子视图中,我有一个名为 _UIPageViewControllerContentView 的对象.
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];
}
}
希望这会有所帮助.
这篇关于UIPageViewController 将先前的 ViewController 保留在视图的背景上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!