2016-12-05 54 views
1

,因爲我們知道,實現PageTabBarController,我們需要在AppDelegate.swift插入這些代碼:Cosmicmind/Material:我如何在不使用AppDelegate.swift的情況下實現PageTabBarController?

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) { 

    } 
} 

回答

0

Linked GitHub Question

嗨,是有一種方法。 UIViewController`,它允許你將它作爲任何其他UIViewController的子項添加。那就是說,你給了我一個好主意。我將創建一個新的UIViewController,它允許您添加儘可能多的子UIViewControllers,這將使這非常容易。我會將其作爲功能請求

在更新之前,請使用建議的將它添加爲子UIViewController的方法。你熟悉如何做到這一點?

+0

感謝,我真的很熟悉添加的UIViewController作爲一個孩子的控制器,但我必須找出另一種替代方案來實現的TabBar(使用的CollectionView )用於容器視圖的標題和UIPageViewController –

+0

只要您有解決方案,就可以了。我目前正在重構PageTabBarController,所以如果你有任何進一步的建議或問題,請讓我知道,謝謝:) – CosmicMind

0

首先創建AppToolbarController(ToolbarController的子類),或者您可以使用材質庫演示中的一個。

然後從您的視圖控制器,你可以使用:

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