简体   繁体   中英

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.

当我尝试从第一个VC向左滚动时,出现空白屏幕。

当我尝试滚动到最后一个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)
    }
}

Add view.backgroundColor = .whiteColor()

in viewDidLoad()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM