我以编程方式在我的iOS应用中写了UIPageViewController
。但是,当我像下面这样设置下一个ViewController时:
pageContainer.setViewControllers([pages[currentIndex + 1]], direction: UIPageViewControllerNavigationDirection.reverse, animated: true, completion: nil)
我的整个应用程序都崩溃了。
现在我发现,这个问题有两个修复:
UIPageViewController
' s transitionStyle
设置为.pageCurl
(这为我提供了令人难以置信的丑陋和不受欢迎的动画)animated
属性设置为false(然后它不会给我任何动画,因此也是不需要的).. 有没有人知道解决这个问题的方法?由于这两个选项都不可能与我的客户一起飞行......
谢谢!
- 编辑
正如所提出的:崩溃信息:
2018-02-25 13:09:25.728651+0100 WAMP[22669:601029] *** Assertion failure in -[UIPageViewController _flushViewController:animated:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UIPageViewController.m:2137
2018-02-25 13:09:25.855963+0100 WAMP[22669:601029] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Don't know about flushed view <UIView: 0x7fcdc5d167c0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000644b90>; layer = <CALayer: 0x60000022daa0>>'
完整的代码示例:
class OnboardingViewController: UIViewController, UIPageViewControllerDelegate {
var pageContainer: UIPageViewController!
var currentIndex: Int? = 0
var pendingIndex: Int?
private(set) lazy var pages: [UIViewController] = {
return [self.newStep(step: "One"), self.newStep(step: "Two"), self.newStep(step: "Three"), self.newStep(step: "Four")]
}()
override func viewDidLoad() {
super.viewDidLoad()
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.setViewControllers([pages[0]], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
view.addSubview(pageContainer.view)
}
@IBAction func nextButtonPressed(_ sender: Any) {
self.pendingIndex = (self.currentIndex!) + 1
self.currentIndex = (self.currentIndex!) + 1
self.pageContainer.setViewControllers([self.pages[self.currentIndex!]], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}
private func newStep(step: String) -> UIViewController {
return UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "onboardingStep\(step)")
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.index(of: pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
}
}
}
答案 0 :(得分:1)
我认为您需要在直接设置视图控制器之前设置UIPageViewController的数据用法。过渡样式.scroll是页面缓存所需的数据源。
override func viewDidLoad() {
super.viewDidLoad()
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([pages[0]], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
view.addSubview(pageContainer.view)
}