2011-05-31 120 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) 

    } 


}