Can not get button to turn uipageviewcontroller page programmatically (Using Pages extension)

I'm fiddling with uipageviewcontroller and I'm trying to go to the next page by pressing a button rather than swiping. I found a neat extension on github called 'Pages' that is supposed to be robust and easy to use, but I can't for the life of me figure this out.

The extension itself comes with previous and next button in the navigation area but I don't want those, I want to be able to press a button on the actual screen and have it go to the next page. Has anyone had any experience with this extension?

AppDelegate

import UIKit
import Pages
import Imaginary



@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    //let pages = pagesControllerInCode()
    let pages = pagesControllerInStoryboard()

    let navigationController = UINavigationController(rootViewController: pages)

    pages.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Previous Page",
      style: .plain,
      target: pages,
      action: #selector(PagesController.moveBack))

    pages.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next Page",
      style: .plain,
      target: pages,
      action: #selector(PagesController.moveForward))

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

    pages.enableSwipe = false
    pages.showBottomLine = false

    return true
  }

  private func pagesControllerInCode() -> PagesController {
    var viewControllers: [UIViewController] = []

    for i in 0..<5 {
      if let imageURL = URL(string: "https://unsplash.it/375/667/?image=\(i+10)") {
        let viewController = ViewController()
        viewController.imageView.setImage(url: imageURL)

        viewControllers.append(viewController)
      }
    }

    let pages = PagesController(viewControllers)

    pages.enableSwipe = false
    pages.showBottomLine = false

    return pages
  }

  private func pagesControllerInStoryboard() -> PagesController {
    let storyboardIds = ["One","Two"]

    return PagesController(storyboardIds)
  }
}

ViewController

import UIKit
import Pages

class ViewController: UIViewController {

  lazy var imageView = UIImageView()

  override func viewDidLoad() {
    super.viewDidLoad()

  }

  override func viewDidLayoutSubviews() {
    //corrects scrollview frame to allow for full-screen view controller pages
    for subView in self.view.subviews {
      if subView is UIScrollView {
        subView.frame = self.view.bounds
        subView.backgroundColor = UIColor.clear
      }
    }
    super.viewDidLayoutSubviews()
  }

  @IBAction func test(_ sender: UIButton) {
    NSLog("Test")
    // This is where I've been trying to figure out how to turn the page programmatically
    }
}

2 answers

  • answered 2017-11-15 00:08 murphguy

    Since Pages is just a subclass of UIPageViewController, that class' usual method for transitioning between controllers it is:

    self.transition(from:_, to: _, duration: _, options: _, animations: _, completion: _)
    

    If you can't seem to work with that library, i'd consider just resorting to using this method. Obviously, you'll have to manage which controllers to transition between on your own, but it could be a valuable thing to learn if you ask me.

  • answered 2017-11-15 05:20 iOS Geek

    Check my answer at - https://stackoverflow.com/a/47278437/6080920

    I had used a pageViewController that is added with multiple ViewController and Disabled scroll gestures that a pageViewController have by default and added some buttons that will allow to navigate user in pageViewController

    Hope it helps you

    Output ScreenShot : With pageViewController embedded with multiple viewController that have UITableView in it

    enter image description here