1

as we know, to implement PageTabBarController, we need to insert these code in 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()
}

Now, i need to use PageTabBarController when i want to open detail for my match data. My question is, how to implement it without insert those code in AppDelegate.swift because it will open my MatchViewController (extend from PageTabBarController) for the first app launch.

I have tried this code, but it will cause Crash, and it pointed to my 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) {

  }
}
Edric
  • 24,639
  • 13
  • 81
  • 91
calvin sugianto
  • 610
  • 7
  • 27

2 Answers2

0

Linked GitHub Question

Hi, yes there is a way. The PageTabBarController is inherited from aUIViewController`, which allows you to add it as a child of any other UIViewController. That said, you just gave me a great idea. I am going to make a new UIViewController that allows you to add as many child UIViewControllers, which will make this super easy to do. I will make this as a Feature Request.

Until the update, please use the suggested method of adding it as a child UIViewController. Are you familiar with how to do that?

CosmicMind
  • 1,499
  • 1
  • 10
  • 6
  • thanks, i`m quite familiar with add UIViewController as a child controller, but i have figure out another alternatives to implement the TabBar (using CollectionView) for the header and UIPageViewController for the container view – calvin sugianto Dec 20 '16 at 10:05
  • Ah okay... as long as you have a solution. I am currently refactoring the PageTabBarController, so if you have any further suggestions or questions, please let me know, thank you :) – CosmicMind Dec 20 '16 at 18:04
0

First create AppToolbarController (subclass of ToolbarController) or you can use the one in the Material library demo.

And then from your view controller, you can use:

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