2011-05-31 110 views
6

我有在我試圖標籤欄項目添加到動態使用UITabBarsetItems方法標籤欄標籤欄基於應用程序。問題添加標籤欄項目UITabBar

下面是代碼:

[self.tabBarController.tabBar setItems:self.level1TabBarItems animated:YES]; 

self.level1TabBarItemsNSMutableArray與4 UITabBarItems它。 當我運行這段代碼時,我從編譯器中得到一個異常。

NSInternalInconsistencyException,原因:不允許直接修改由選項卡欄控制器管理的選項卡欄。

我試過刪除UITabBarViewController並再次添加它,但它沒有工作。

回答

14

The documentation明確規定,你不應該直接修改標籤欄。改爲使用setViewControllers:animated:

1

AFAIK你不能代替的TabBar。這是Apple不允許的。我現在檢查一下。

你可以做的,雖然是什麼,是創造一個segmentedController和restyle它看起來像一個的TabBar。它具有幾乎相同的用途。

編輯:上面的忍者海報說:你不能交替使用TabBar。我會建議分段控制器。

2

我希望可以幫助您與下面的代碼:

func application(_application: UIApplication, 
       didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{ 
    // Override point for customization after application launch. 

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

    return true 
} 
import UIKit 


class LongUITabBarController: UITabBarController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let vc1 = VC1_ViewController() 
     let vc2 = VC2_ViewController() 
     let vc3 = VC3_ViewController() 
     let vc4 = VC4_ViewController() 

     vc1.tabBarItem = UITabBarItem(title: "LIST ALL", image: UIImage(named: "list"), tag: 1) 
     vc2.tabBarItem = UITabBarItem(title: "BEST CELLER", image: UIImage(named: "bestCeller"), tag: 2) 
     vc3.tabBarItem = UITabBarItem(title: "MOST LIKE", image: UIImage(named: "like"), tag: 3) 
     vc4.tabBarItem = UITabBarItem(title: "NEW", image: UIImage(named: "new"), tag: 4) 

     viewControllers = [vc1, vc2, vc3, vc4] 
     setViewControllers(viewControllers, animated: true) 

     // backGround for tapBarView 
     tabBar.barTintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1) 

    } 


} 
相關問題