无法获取按钮以编程方式打开uipageviewcontroller页面(使用Pages扩展名)

时间:2017-11-14 23:39:06

标签: ios swift uipageviewcontroller

我正在摆弄uipageviewcontroller而我正试图通过按下按钮而不是滑动来转到下一页。我在github called 'Pages'上发现了一个整洁的扩展,它应该是健壮且易于使用的,但我不能为我的生活弄清楚这一点。

扩展程序本身带有导航区域中的上一个和下一个按钮,但我不想要那些,我希望能够按下实际屏幕上的按钮并让它转到下一页。有没有人有这种扩展的经验?

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 个答案:

答案 0 :(得分:0)

由于Pages只是UIPageViewController的子类,因此该类在控制器之间转换的常用方法是:

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

如果您似乎无法使用该库,我会考虑使用此方法。显然,你必须自己管理哪些控制器之间的转换,但如果你问我,这可能是一件有价值的事情。

答案 1 :(得分:0)

在 - https://stackoverflow.com/a/47278437/6080920

检查我的回答

我使用了一个pageViewController,它添加了多个ViewController和Disabled滚动手势,一个pageViewController默认具有这个功能,并添加了一些按钮,允许在pageViewController中导航用户

希望它可以帮到你

输出ScreenShot:使用嵌入了多个viewController的pageViewController,其中包含UITableView

enter image description here