Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem adding tab bar items to UITabBar

I have a tab bar based application in which I am trying to add tab bar items to the tab bar dynamically using setItems method of the UITabBar.

Here is the code:

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

Where self.level1TabBarItems is an NSMutableArray with 4 UITabBarItems in it. When I run this code, I get an exception from the compiler.

NSInternalInconsistencyException, reason:Directly modifying a tab bar managed by a tab bar controller is not allowed.

I have tried deleting the UITabBarViewController and adding it again but it did not work.

like image 463
Aqueel Avatar asked May 31 '11 06:05

Aqueel


People also ask

How do I add items to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

How do I add an image to the tab bar in iOS?

iOS UITabBarController Changing Tab Bar Item Title and Icon For a custom icon, add the required images to the assets folder and set the 'System Item' from earlier to 'custom'. Now, set the icon to be shown when the tab is selected from the 'selected image' drop down and the default tab icon from the 'image' drop down.

How do I create a custom tab bar in Swift?

Implement a view controller that can hold some other view controllers. Show one of those view controllers. Show a tab bar at the bottom of the screen over the shown view controller. Switch between the various view controllers when the user taps on a tab bar button.


3 Answers

The documentation clearly states that you shouldn't modify the tab bar directly. Use setViewControllers:animated: instead.

like image 154
Deepak Danduprolu Avatar answered Oct 04 '22 02:10

Deepak Danduprolu


I hope can help you with following code:

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)

    }


}
like image 33
user7352457 Avatar answered Oct 02 '22 02:10

user7352457


AFAIK you can't replace the tabbar. It's not allowed by Apple. I'll check it now.

What you can do though, is creating a segmentedController and restyle it to look like a tabbar. It has pretty much the same use.

EDIT: Above, ninja poster said it: you can't alternate the tabbar. I'd suggest the segmented controller.

like image 38
Joetjah Avatar answered Oct 03 '22 02:10

Joetjah