PageViewController 在旋转时崩溃
Posted
技术标签:
【中文标题】PageViewController 在旋转时崩溃【英文标题】:PageViewController crashes on rotation 【发布时间】:2016-03-13 11:23:21 【问题描述】:您好,我有一个 pageViewcontroller,里面有一些 viewcontroller。里面有一个空白控制器,里面有一个标签
问题是当我尝试旋转它时它崩溃了
这里是代码
class FAPageViewController: UIPageViewController , UIPageViewControllerDataSource, UIPageViewControllerDelegate
var pages = ["one", "two"]
var pageViewController = UIPageViewController()
override func viewDidLoad()
super.viewDidLoad()
pages.removeAll()
for (var i = 0; i < 4; i++)
var obj = "child"
pages.append(obj)
pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
pageViewController.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 64, self.view.frame.width, self.view.frame.height - 64)
addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
pageViewController.delegate = self
pageViewController.dataSource = self
pageViewController.setViewControllers([viewcontrollerAtIndex(0)], direction: .Forward, animated: true, completion: nil)
pageViewController.didMoveToParentViewController(self)
print(pages)
func viewcontrollerAtIndex(index: Int) -> UIViewController
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
return vc!
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
if let index = pages.indexOf(viewController.restorationIdentifier!)
if index < pages.count - 1
return viewcontrollerAtIndex(index + 1)
return nil
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
if let index = pages.indexOf(viewController.restorationIdentifier!)
if index > 0
return viewcontrollerAtIndex(index - 1)
return nil
这是控制台日志
2016-03-13 14:19:16.743 FacebookAlbums[7550:1624956] * 断言失败 -[FacebookAlbums.FAPageViewController willAnimateRotationToInterfaceOrientation:duration:], /BuildRoot/Library/Caches/com.apple.xbs/来源/UIKit_Sim/UIKit-3512.30.14/UIPageViewController.m:1062 2016-03-13 14:19:16.748 FacebookAlbums[7550:1624956] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“没有视图控制器”
【问题讨论】:
【参考方案1】:我遇到了同样的问题。这就是我解决它的方法:通过将以下方法添加到 UIPageViewController 实现文件,我再次设置了 UIPageViewController(就像我们第一次那样)以防设备旋转:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
因为大多数时候,UIPageViewController 是模态呈现的,所以在设备旋转时,会再次调用这个控制器。
【讨论】:
这对我有用,谢谢你,在 swift 4.1 中工作。不知道为什么。只需要添加这个空方法【参考方案2】:我还有一个继承 UIPageViewController 的应用程序。我多年来一直在使用它,但我们总是将其限制为肖像模式。我最近开始了另一个以此为基础的项目,但在这种情况下,我们确实希望允许横向模式。该应用程序有一个导航控制器和几个初始屏幕,然后推送到 PageViewController。旋转可以与导航控制器顶部的传统 VC 一起使用,但是如果我尝试在显示 PageViewController 的情况下进行旋转,我会得到一个异常:
2020-07-24 19:33:01.440876-0400 MAT-sf[68574:12477921] *** 断言失败 -[MAT_sf.SurveyVC willAnimateRotationToInterfaceOrientation:duration:], /Library/Caches/com.apple.xbs /Sources/UIKitCore_Sim/UIKit-3920.31.102/UIPageViewController.m:1055 2020-07-24 19:33:01.454443-0400 MAT-sf[68574:12477921] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“没有视图控制器”
事实证明,我所要做的就是为我的子类添加一个覆盖:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
super.viewWillTransition(to: size, with: coordinator)
它所做的只是调用超类中的相同函数。我猜如果您继承 PageViewController,这应该是必需的方法。
希望这对某人有所帮助。在我偶然发现解决方案之前,我花了大约一天的时间进行研究。
【讨论】:
这种方法解决了我的问题。目前尚不清楚它为什么有效,但我很感激它确实有效。【参考方案3】:感谢 HSH,我在 Xamarin 中遇到了同样的问题。 我实现了:
public override void WillAnimateRotation(UIInterfaceOrientation toInterfaceOrientation, double duration)
将其留空,防止它调用基方法。这显然会导致它崩溃。 在不调用基本方法的情况下实现此覆盖方法对我有用。我在快速改变方向时也遇到了类似的异常。
(MonoTouchException) Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: No view controllers
Native stack trace:
0 CoreFoundation 0x0000000113e3302e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x0000000114b7cb20 objc_exception_throw + 48
2 CoreFoundation 0x0000000113e32da8 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00000001113a3b61 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00000001208b32c6 -[UIPageViewController willAnimateRotationToInterfaceOrientation:duration:] + 3983
5 UIKitCore 0x0000000120960d3e -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 294
6 UIKitCore 0x000000012095d060 -[_UIViewControllerTransitionContext __runAlongsideAnimations] + 263
7 UIKitCore 0x000000012148862a +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 528
8 UIKitCore 0x0000000121488bd9 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 99
9 UIKitCore 0x0000000120975276 __58-[_UIWindowRotationAnimationController animateTransition:]_block_invoke_2 + 278
10 UIKitCore 0x000000012148c788 +[UIView(Internal) _performBlockDelayingTriggeringResponderEvents:forScene:] + 174
11 UIKitCore 0x0000000120975004 __58-[_UIWindowRotationAnimationController animateTransition:]_block_invoke + 164
12 UIKitCore 0x000000012148862a +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 528
13 UIKitCore 0x0000000121488bd9 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 99
14 UIKitCore 0x0000000120974edc -[_UIWindowRotationAnimationController animateTransition:] + 491
15 UIKitCore 0x0000000120fefef9 -[UIWindow _rotateToBounds:withAnimator:transitionContext:] + 525
16 UIKitCore 0x0000000120ff293c -[UIWindow _rotateWindowToOrientation:updateStatusBar:duration:skipCallbacks:] + 2331
17 UIKitCore 0x0000000120ff2f2f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 633
18 UIKitCore 0x0000000120ff1e83 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 119
19 UIKitCore 0x0000000120ff0d00 __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 111
20 UIKitCore 0x0000000120ff0c29 -[UIWindow _updateToInterfaceOrientation:duration:force:] + 455
21 Foundation 0x0000000111426976 __NSFireDelayedPerform + 420
22 CoreFoundation 0x0000000113d96944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
23 CoreFoundation 0x0000000113d96632 __CFRunLoopDoTimer + 1026
24 CoreFoundation 0x0000000113d95c8a __CFRunLoopDoTimers + 266
25 CoreFoundation 0x0000000113d909fe
【讨论】:
以上是关于PageViewController 在旋转时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Android 片段不保存状态,在旋转/屏幕锁定/返回时崩溃