Cosmicmind / Material:如何在不使用AppDelegate.swift的情况下实现PageTabBarController?

时间:2016-12-05 09:23:16

标签: swift3 xcode8 ios10 material cosmicmind

我们知道,要实现PageTabBarController,我们需要在AppDelegate.swift中插入这些代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: 
    let viewControllers = [MatchDetailViewController(),ListPlayersViewController(),ChatViewController()]

    window = UIWindow(frame: Device.bounds)
    window!.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)
    window!.makeKeyAndVisible()
}

现在,当我想打开匹配数据的详细信息时,我需要使用PageTabBarController。我的问题是,如何实现它而不在AppDelegate.swift中插入这些代码,因为它将在第一次应用程序启动时打开我的MatchViewController(从PageTabBarController扩展)。

我已经尝试过这段代码,但它会导致崩溃,并且它指向我的AppDelegate.swift

class MatchViewController: PageTabBarController {
    var window: UIWindow?

override func viewDidLoad() {
    super.viewDidLoad()

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

open override func prepare() {
    super.prepare()
    let viewControllers = [MatchDetailViewController(),ListPlayersViewController(),ChatViewController()]
    //1st try: Crash
    window = UIWindow(frame: Device.bounds)
    window!.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)
    window!.makeKeyAndVisible()

    //2nd try: error
    self.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)

    //3rd try: crash
    self.viewControllers = viewControllers

    delegate = self
    preparePageTabBar()
}

fileprivate func preparePageTabBar() {
    pageTabBar.lineColor = Color.blue.base        
    pageTabBar.dividerColor = Color.blueGrey.lighten5

    pageTabBarAlignment = PageTabBarAlignment.top
    pageTabBar.lineAlignment = TabBarLineAlignment.bottom

}
}

extension MatchViewController: PageTabBarControllerDelegate {
func pageTabBarController(_ pageTabBarController: PageTabBarController, didTransitionTo viewController: UIViewController) {

  }
}

2 个答案:

答案 0 :(得分:0)

Linked GitHub Question

嗨,是的,有办法。 PageTabBarController is inherited from a UIViewController`,它允许您将其添加为任何其他UIViewController的子项。那就是说,你刚才给了我一个好主意。我将创建一个新的UIViewController,允许您添加尽可能多的子UIViewControllers,这将使这非常容易。我将此作为功能请求

在更新之前,请使用建议的方法将其添加为子UIViewController。你熟悉怎么做吗?

答案 1 :(得分:0)

首先创建AppToolbarController(ToolbarController的子类),或者您可以使用Material库演示中的那个。

然后从您的视图控制器中,您可以使用:

DispatchQueue.main.async {
let tabbarViewController = AppPageTabbarController(viewControllers: [vc1,vc2,vc3], selectedIndex: 0)
self.present(AppToolbarController(rootViewontroller: tabbarViewController))
}