0

I am currently having issues on removing the background (the bar at the bottom of the screen) like the problem that they had exactly in this post

However, the solutions posted were all in Objective-c. I tried to translate it to Swift, but it didn't work.

I also tried to change "UIPageControl.appearance()" to cleancolor, but it still didn't work.

Anybody can provide some hints?

Thanks!

My code here:

import UIKit

class TutorialViewController: UIViewController, UIPageViewControllerDataSource{

var pageViewController: UIPageViewController?
let contentImages = ["Tutorial1.png", "Tutorial2.png","Tutorial3.png"]


override func viewDidLoad() {
    super.viewDidLoad()
    createPageViewController()
    setupPageControl()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

    //supporting functions
private func createPageViewController() {

    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
    pageController.dataSource = self

    if contentImages.count > 0 {
        let firstController = getItemController(0)!
        let startingViewControllers: NSArray = [firstController]
        pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    pageViewController = pageController
    addChildViewController(pageViewController!)
    self.view.addSubview(pageViewController!.view)
    pageViewController!.didMoveToParentViewController(self)
}

private func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.clearColor()
}

// MARK: - UIPageViewControllerDataSource

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemViewController

    if itemController.itemIndex > 0 {
        return getItemController(itemController.itemIndex-1)
    }

    return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemViewController

    if itemController.itemIndex+1 < contentImages.count {
        return getItemController(itemController.itemIndex+1)
    }

    return nil
}

private func getItemController(itemIndex: Int) -> PageItemViewController? {

    if itemIndex < contentImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("TutorialItemController")as! PageItemViewController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = contentImages[itemIndex]
        return pageItemController
    }

    return nil
}

// MARK: - Page Indicator

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return contentImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}

override func prefersStatusBarHidden() -> Bool {

    return true;
}

}
Community
  • 1
  • 1
Ares Li
  • 603
  • 1
  • 7
  • 19

2 Answers2

3

Try removing this method

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}

It works fine for me in swift

Vadim Popov
  • 1,177
  • 8
  • 17
1

If you check in the example link you have provided, i found some difference which you may help you in the implementation

1. // We need to cover all the control by making the frame taller (+ 37)

[[self.pageController view] setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, [[self view] bounds].size.height + 37)];

- Added extra height in the PageViewController so default page controller bottom bar is hidden

// Bring the common controls to the foreground (they were hidden since the frame is taller)

2.

[self.view bringSubviewToFront:self.pcDots];

The trick is put the PageControl in the foreground at bottom. put UIPageControl and set the value of it in the delegate method of UIPageController


If you need the full demo implementation of UIPageController you can check the tutorial here:https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/

At the end of this tutorial you will get the custom UIPageController which you want, if you want to set the page control at bottom change the position of UIPageControl in the TutorialViewController in storyboard

HardikDG
  • 5,892
  • 2
  • 26
  • 55