我使用UIPageViewController滚动浏览3个视图控制器。目标是能够向左和向右滑动以导航它们,我可以使用以下代码进行操作。问题是,在第一次打开应用程序时,导航栏会加载到错误的位置,需要一整秒才能符合约束条件。导航栏的负载高于应有的值。我想知道为什么加载会有这样的延迟。
class ViewController: UIViewController, UIPageViewControllerDataSource {
var myViewControllers = Array(count: 3, repeatedValue:UIViewController())
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let pvc = segue.destinationViewController as! UIPageViewController
pvc.dataSource = self
let storyboard = UIStoryboard(name: "Main", bundle: nil);
var vc0 = storyboard.instantiateViewControllerWithIdentifier("ViewController1") as! UIViewController
var vc1 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! UIViewController
var vc2 = storyboard.instantiateViewControllerWithIdentifier("ViewController3") as! UIViewController
self.myViewControllers = \[vc0, vc1, vc2\]
pvc.setViewControllers(\[myViewControllers\[1\]\], direction:.Forward, animated:false, completion:nil)
println("Loaded")
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var currentIndex = find(self.myViewControllers, viewController)!+1
if currentIndex >= self.myViewControllers.count {
return nil
}
return self.myViewControllers\[currentIndex\]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var currentIndex = find(self.myViewControllers, viewController)!-1
if currentIndex < 0 {
return nil
}
return self.myViewControllers\[currentIndex\]
}