Emil David Emil David - 4 months ago 32
Swift Question

UIPageViewController Blank Screen

I am using a

UIPageViewController
to load multiple
UIViewControllers
but every time I try scrolling past the last
ViewController
or even try scrolling the opposite direction on the start of the
pageController
I get a blank screen as shown below. The blank view is never really fully loaded onto the view but ideally I would like the blank screen to simply have a white colour background hence not obviously visible to the user.

The blank screen appears when I try scrolling to the left from the first VC.

The blank screen appears again when I try scrolling past the last VC.

Any help will be greatly appreciated.

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)
}
}

Answer

Add view.backgroundColor = .whiteColor()

in viewDidLoad()