UIPageViewController Blank Screen - ios

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.
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()

Related

Swipe direction PageViewController

My problem here it's the swipe movement when I use the pageViewController implementation. I want it to go from right to left but it's going from left to right who's not naturally for the UI experience !
It was working perfectly when I was using Xcode 7 and Swift 2.2 ...
So since I copied/pasted my code and modifiying with the Xcode 8 suggestion, it has this strange behavior.
import UIKit
class FirstEventViewController: UIViewController {
// FIXME: - The swipe gesture has to be from right to left when the view appears (PageControl not showing)
var pageViewController: UIPageViewController!
var titleEvents = [String]()
var pageImages: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
pageImages = NSArray(objects: "supersmashbros","spiritodjsession","thomasdelortrio")
titleEvents = ["supersmashbros","spiritodjsession","thomasdelortrio"]
initiatePgVC()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//TODO: CODE HERE
}
// MARK: - Private functions
/**
Function initializing the logic of the pageviewController
*/
func initiatePgVC() {
pageViewController = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController
pageViewController.dataSource = self
let startVC = viewControllerAtIndex(index: 0)
let viewControllers = NSArray(object: startVC) as! [UIViewController]
pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
//pageViewController.view.frame = CGRect(x:0, y:65, width:self.view.frame.width, height:self.view.frame.size.height - 140)
pageViewController.view.frame = self.view.bounds
addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController.didMove(toParentViewController: self)
}
/**
Function associating the viewControllers
*/
func viewControllerAtIndex(index: Int)->SecondEventViewController{
if ((titleEvents.count == 0) || (index >= titleEvents.count)){
return SecondEventViewController()
}
let vc:SecondEventViewController = storyboard?.instantiateViewController(withIdentifier: "SecondEventViewController") as! SecondEventViewController
vc.imageFile = pageImages[index] as! String
vc.labelTitle = titleEvents[index]
vc.pageIndex = index
return vc
}
}
extension FirstEventViewController:UINavigationBarDelegate{
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}
extension FirstEventViewController:UIPageViewControllerDataSource{
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound){
return nil
}
index = index-1
return viewControllerAtIndex(index: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == NSNotFound){
return nil
}
index = index+1
if (index == titleEvents.count){
return nil
}
return viewControllerAtIndex(index: index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return titleEvents.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
I once suspected the pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil) because it implements the direction but even if I change with reverse, nothing happens.
Have you got the changing of the indexes the wrong way around?
for after controller i'd expect the index to increase but you are decreasing it, and vice versa.
Try swapping these, so after should be + 1 and before -1
index = index+1

BAD_ACCESS on UIPageViewController

I've got a working UIPageViewController that holds multiple UIViewControllers embedded in an UINavigationController and each UIViewController has a preview of an array of images wich, when opened, instantiate a new UIPageViewController to display those images
when i swipe through the images and then swipe back to the first one my app crashes with "EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" same thing when i use the back button of the UINavigationController
why is that and how can i fix this ?
My PageViewController (the marked line is the last one i got in the debugger before it crashes):
class DetailPageMasterViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
{
var presentationPageIndex: Int = 0
var itemsArray = [Aktion]()
var pageViewController: UIPageViewController!
#IBOutlet weak var btnEditOutlet: UIBarButtonItem!
#IBAction func btnEditAction(sender: AnyObject)
{
}
override func viewDidLoad()
{
super.viewDidLoad()
self.pageViewController = UIPageViewController.init(transitionStyle: .Scroll,
navigationOrientation: .Horizontal,
options: nil)
self.pageViewController.delegate = self
self.pageViewController.dataSource = self
self.presentationPageIndex = 0
let firstVC = self.viewControllerAtIndex(presentationPageIndex)
let viewControllers = [firstVC]
self.pageViewController.setViewControllers(viewControllers,
direction: .Forward,
animated: false,
completion: nil)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
self.setupPageControl()
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
{
if completed
{
let minionVC = self.pageViewController.viewControllers?.last as! DetailMinionViewController
presentationPageIndex = minionVC.pageIndex
}
}
func viewControllerAtIndex(index: Int) -> DetailMinionViewController
{
let contentVC = self.storyboard?.instantiateViewControllerWithIdentifier("MinionPageViewController") as! DetailMinionViewController
contentVC.aktion = itemsArray[index]
contentVC.pageIndex = index
return contentVC
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
if let viewController = viewController as? DetailMinionViewController
{
var index = viewController.pageIndex
if index == 0 || index == NSNotFound
{
return nil //MARKED LINE
}
index -= 1
return self.viewControllerAtIndex(index)
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
if let viewController = viewController as? DetailMinionViewController
{
var index = viewController.pageIndex
if index == NSNotFound
{
return nil
}
index += 1
if index == NSNotFound || index >= itemsArray.count
{
return nil
}
return self.viewControllerAtIndex(index)
}
return nil
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return itemsArray.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return presentationPageIndex
}
func setupPageControl()
{
UIPageControl.appearance().backgroundColor = UIColor.clearColor()
UIPageControl.appearance().pageIndicatorTintColor = UIColor.whiteColor()
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
}
}
So the structure looks like
UINavigationController -> DetailPageMasterViewController -> DetailMinionViewController -> PicturesPageMasterViewController -> PicturesMinionViewController
So i finally figured out what caused my app to crash.
Long story short:
a gestureRecognizer in the PicturesMinionViewController tried to access an already deinitialized ImageView, my pageViewController worked fine
Please check the number of viewController added into the PageController. If possible you can share your code.

Error: Page Control jump twice with single page swap

With the following code, the page control has jumped twice instead of once during a single page swap. I need to add the UIPageViewController as subview as below, how can I make the pagecontrol act right? Thank you!
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
var pageViewController: UIPageViewController!
let pagesArray = ["Page1ViewController", "Page2ViewController","Page3ViewController"]
let pageControl : UIPageControl = UIPageControl(frame: CGRectMake(100, 600, 200, 20))
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier (pagesArray[index])
return vc
}
func pageViewController(pageViewController:UIPageViewController,
viewControllerBeforeViewController
viewController: UIViewController) -> UIViewController?{
if var index = pagesArray.indexOf(viewController.restorationIdentifier!){
if index > 0 {
index--
self.pageControl.currentPage = index
return viewControllerAtIndex(index)
}
}
return nil
}
func pageViewController(pageViewController:UIPageViewController,
viewControllerAfterViewController
viewController: UIViewController) -> UIViewController?{
if var index = pagesArray.indexOf(viewController.restorationIdentifier!){
if index < pagesArray.count - 1 {
index++
return viewControllerAtIndex(index)
}
}
return nil
}
override func viewDidLoad() {
super.viewDidLoad()
pageControl.numberOfPages = 3
pageControl.currentPage = 0
pageControl.tintColor = UIColor.redColor()
pageControl.pageIndicatorTintColor = UIColor.blackColor()
pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
if let vc = storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController")
{
self.addChildViewController(vc)
self.view.addSubview(vc.view)
self.view.addSubview(pageControl)
pageViewController = vc as! UIPageViewController
pageViewController.dataSource = self
pageViewController.delegate = self
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil)
pageViewController.didMoveToParentViewController(self)
}
}
}
try this
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let identifier = viewController.restorationIdentifier
self.index = self.identifiers.indexOfObject(identifier!)
if index == identifiers.count - 1 {
return nil
}
//increment the index to get the viewController before the current one
self.index = self.index + 1
return self.viewControllerAtIndex(self.index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let identifier = viewController.restorationIdentifier
self.index = self.identifiers.indexOfObject(identifier!)
if index == 0 {
return nil
}
//decrement the index to get the viewController before the current one
self.index = self.index - 1
return self.viewControllerAtIndex(self.index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.identifiers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}

Swift, protocol and variable values not stored...?

I'm new to protocols and pageview controllers and I tried to something I hoped was simple. To pass a variable value from the content viewcontroller to the page list viewcontroller but it doesn't work so I'm doing something wrong...
In my storyboard I have a ViewController (as the Page List viewcontroller) and embedded in a navigation controller. The navigation bar has a button on it.
There is a PageContentViewController that has a label.
The code I use follows (EDITED with the accepted solution provided by #Tom Elliott):
protocol setRoomOnScreenProtocol {
func updateRoomOnScreen(myRoom: String)
}
class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, setRoomOnScreenProtocol {
#IBAction func press1(sender: AnyObject) {
println("roomOnScreen: \(roomOnScreen)")
println("pageTitles: \(pageTitles)")
}
#IBOutlet weak var btn1: UIBarButtonItem!
let pageTitles = ["Room 1", "Room 2", "Room 3", "Room 4"]
var count = 0
var pageViewController : UIPageViewController!
var roomOnScreen: String = ""
override func viewDidLoad() {
super.viewDidLoad()
reset()
}
func updateRoomOnScreen(myRoom: String) {
roomOnScreen = myRoom
println("roomOnScreen (PGL): \(roomOnScreen)")
}
func reset() {
/* Getting the page View controller */
pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let pageContentViewController = self.viewControllerAtIndex(0)
self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index = (viewController as! PageContentViewController).pageIndex!
index++
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index = (viewController as! PageContentViewController).pageIndex!
if (index <= 0) {
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func viewControllerAtIndex(index : Int) -> UIViewController? {
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return nil
}
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
pageContentViewController.delegate = self
pageContentViewController.titleText = self.pageTitles[index]
pageContentViewController.pageIndex = index
return pageContentViewController
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pageTitles.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
and the PageContentViewController:
class PageContentViewController: UIViewController {
var pageIndex: Int?
var titleText : String!
var delegate: setRoomOnScreenProtocol!
#IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.label1.text = self.titleText
}
override func viewDidAppear(animated: Bool) {
if let actualDelegate = self.delegate {
actualDelegate.updateRoomOnScreen(titleText)
} else {
println("error")
}
doMore()
}
func doMore() {
println("showing...:\(titleText)")
}
}
When I move through the pages I correctly get the output:
roomOnScreen (PGL): Room 1
showing...:Room 1
roomOnScreen (PGL): Room 2
showing...:Room 2
But when I hit the navigation bar button I get no value...
roomOnScreen:
pageTitles: [Room 1, Room 2, Room 3, Room 4]
What am I doing wrong...?
Thanks!
The line:
self.delegate = ViewController()
Is actually creating a brand new instance of ViewController each time it is called, so the original ViewController that is visible is not receiving the message.
I'd suggest setting the delegate when you initially create the pages:
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
pageContentViewController.delegate = self

UIPageViewController doesn't stop scroll

I am Using UIPageController having 2 images in it. While running the app, The UIPageController shows the first page perfectly, but then scrolls infinitely from the second page. It maintains the same image and continues to show the similar image even after it has reached the limit. It only changes the image for the first swipe, and then neither changes the image nor revert back to the initial image on the subsequent scroll.
Given is the source. Please help.
import UIKit
class SignUpViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController : UIPageViewController!
var InfoImages : NSArray!
override func viewDidLoad() {
super.viewDidLoad()
self.InfoImages = NSArray(objects: "blue","red")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("IntroPageController") as! UIPageViewController
self.pageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(0) as ContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(index: Int) -> ContentViewController
{
if ((self.InfoImages.count == 0) || (index >= self.InfoImages.count)){
return ContentViewController()
}
let info = storyBoard.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
info.imageFile = self.InfoImages[index] as! String
return info
}
//MARK : -DataSource
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if( index == 0 || index == NSNotFound ){
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if( index == NSNotFound ){
return nil
}
index++
if(index == self.InfoImages.count){
return nil
}
return self.viewControllerAtIndex(index)
}
#available(iOS 6.0, *)
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.InfoImages.count
}
#available(iOS 6.0, *)
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}
#IBAction func SkipInfo(sender: AnyObject) {
let skipinfo = storyBoard.instantiateViewControllerWithIdentifier("afterLoginview") as! LoginViewController
self.presentViewController(skipinfo , animated: true, completion: nil)
}
Cheers and Thanks in advance!
P.S : I am using Xcode 7.0 beta (swift 2.0)

Resources