UIPageViewController 空白屏幕
UIPageViewController Blank Screen
我正在使用 UIPageViewController
加载多个 UIViewControllers
但每次我尝试滚动到最后一个 ViewController
甚至尝试在 [= 开始时向相反方向滚动14=] 我得到一个空白屏幕,如下所示。空白视图从未真正完全加载到视图上,但理想情况下我希望空白屏幕仅具有白色背景,因此用户不会明显看到。
任何帮助将不胜感激。
import UIKit
class MainPageVC: UIPageViewController
{
var totalPages: [UIViewController] = [UIViewController]()
@IBOutlet weak var rightBarButtonItem: UIBarButtonItem!
override func viewDidLoad()
{
super.viewDidLoad()
let attributes: [String : AnyObject] = [NSFontAttributeName: Constants.defaultFont]
rightBarButtonItem.setTitleTextAttributes(attributes, forState: .Normal)
self.delegate = self
self.dataSource = self
let eyeWearVC = storyboard!.instantiateViewControllerWithIdentifier("eyeWear") as! EyeWearVC
let fitnessVC = storyboard!.instantiateViewControllerWithIdentifier("fitness") as! FitnessVC
let healthVC = storyboard!.instantiateViewControllerWithIdentifier("health") as! HealthVC
let environmentVC = storyboard!.instantiateViewControllerWithIdentifier("environment") as! EnvironmentVC
totalPages.append(eyeWearVC)
totalPages.append(fitnessVC)
totalPages.append(healthVC)
totalPages.append(environmentVC)
setViewControllers([eyeWearVC], direction: .Forward, animated: true, completion: nil)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func viewControllerAtIndex(index: Int)-> UIViewController
{
if self.totalPages.count == 0 || index >= self.totalPages.count
{
return UIViewController()
}
return totalPages[index]
}
extension MainPageVC: UIPageViewControllerDelegate
{
}
extension MainPageVC: UIPageViewControllerDataSource
{
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return totalPages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == 0 || currentIndex == NSNotFound
{
return nil
}
currentIndex -= 1
return self.viewControllerAtIndex(currentIndex)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == NSNotFound
{
return nil
}
currentIndex += 1
if currentIndex == totalPages.count
{
return nil
}
return self.viewControllerAtIndex(currentIndex)
}
}
添加
view.backgroundColor = .whiteColor()
在viewDidLoad()
我正在使用 UIPageViewController
加载多个 UIViewControllers
但每次我尝试滚动到最后一个 ViewController
甚至尝试在 [= 开始时向相反方向滚动14=] 我得到一个空白屏幕,如下所示。空白视图从未真正完全加载到视图上,但理想情况下我希望空白屏幕仅具有白色背景,因此用户不会明显看到。
任何帮助将不胜感激。
import UIKit
class MainPageVC: UIPageViewController
{
var totalPages: [UIViewController] = [UIViewController]()
@IBOutlet weak var rightBarButtonItem: UIBarButtonItem!
override func viewDidLoad()
{
super.viewDidLoad()
let attributes: [String : AnyObject] = [NSFontAttributeName: Constants.defaultFont]
rightBarButtonItem.setTitleTextAttributes(attributes, forState: .Normal)
self.delegate = self
self.dataSource = self
let eyeWearVC = storyboard!.instantiateViewControllerWithIdentifier("eyeWear") as! EyeWearVC
let fitnessVC = storyboard!.instantiateViewControllerWithIdentifier("fitness") as! FitnessVC
let healthVC = storyboard!.instantiateViewControllerWithIdentifier("health") as! HealthVC
let environmentVC = storyboard!.instantiateViewControllerWithIdentifier("environment") as! EnvironmentVC
totalPages.append(eyeWearVC)
totalPages.append(fitnessVC)
totalPages.append(healthVC)
totalPages.append(environmentVC)
setViewControllers([eyeWearVC], direction: .Forward, animated: true, completion: nil)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func viewControllerAtIndex(index: Int)-> UIViewController
{
if self.totalPages.count == 0 || index >= self.totalPages.count
{
return UIViewController()
}
return totalPages[index]
}
extension MainPageVC: UIPageViewControllerDelegate
{
}
extension MainPageVC: UIPageViewControllerDataSource
{
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return totalPages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == 0 || currentIndex == NSNotFound
{
return nil
}
currentIndex -= 1
return self.viewControllerAtIndex(currentIndex)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
var currentIndex: Int = totalPages.indexOf(viewController)!
if currentIndex == NSNotFound
{
return nil
}
currentIndex += 1
if currentIndex == totalPages.count
{
return nil
}
return self.viewControllerAtIndex(currentIndex)
}
}
添加
view.backgroundColor = .whiteColor()
在viewDidLoad()