Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UITabBar in iphone using objective c

How to add add UITabBar programmatically for iphone app. Need some suggestion and sample code.

like image 888
Vishnu Avatar asked Feb 21 '11 05:02

Vishnu


1 Answers

Here the two aspects....

First Aspect (UITabBarController): Here You can add multiple classes (i.e Viewcontrollers) to the UITabBarController as show in the previous answers...

Second Aspect (UITabbar): Here You can add the Tabbar in any view controller...And for this Tabbar you can add multiple UITabBar Items..and You can get the individual Tabbar Item Action.....

See the Below Code For UITabBar

---------> Adding UITabBar

UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
myTabBar.delegate=self;   //here you need import the protocol <UITabBarDelegate>
[self.view addSubview:myTabBar];

---------> Adding Items to UITabBar

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];

UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:[UIImage imageNamed:@"Item1image.png"] tag:0];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1 ];
UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2 ];

[tabBarItems addObject:tabBarItem1];
[tabBarItems addObject:tabBarItem2];
[tabBarItems addObject:tabBarItem3];

myTabBar.items = tabBarItems;
myTabBar.selectedItem = [tabBarItems objectAtIndex:0];

---------> Getting The Item Actions in a Delegate Method

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectedTag = tabBar.selectedItem.tag;
    NSLog(@"%ld",(long)selectedTag);
    if (selectedTag == 0) {
        //Do what ever you want here
    } else if(selectedTag == 1) {
        //Do what ever you want
    } else { //if(selectedTag == 2)
        //Do what ever you want here
    }
}



Update: (Swift 2.0)

1. First Aspect (UITabBarController):
This will allow us to configure UIViewControllers as tabbar items in UITabBarController.
Below is the basic approach for UITabBarController:

---------> Adding UITabBarController to window in appdelegate

let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.delegate = self // Need to import 'UITabBarControllerDelegate'
self.window!.rootViewController = tabBarController;
self.window?.makeKeyAndVisible();

---------> Configuring UIViewControllers

let firstViewController = UIViewController()
firstViewController.title = "FirstVC"
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let firstNavController: UINavigationController = UINavigationController(rootViewController: firstViewController)

let secondViewController = UIViewController()
secondViewController.title = "SecondVC"
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let secondNavController: UINavigationController = UINavigationController(rootViewController: secondViewController)

let thirdViewController = UIViewController()
thirdViewController.title = "ThirdVC"
thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let thirdNavController: UINavigationController = UINavigationController(rootViewController: thirdViewController)

let forthViewController = UIViewController()
forthViewController.title = "ForthVC"
forthViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
let forthNavController: UINavigationController = UINavigationController(rootViewController: forthViewController)

---------> Attaching UIViewControllers to UITabBarController

tabBarController.viewControllers = [firstNavController, secondNavController, thirdNavController, forthNavController]

---------> Receiving event in 'UITabBarControllerDelegate'

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("didSelectViewController: \(viewController.title) ?")
}


2. Second Aspect (UITabBar):
This will allow us to create tabbar items. We can get event for individual item in delegate method
Below is the basic approach for UITabBar:

---------> Adding UITabBar to a view

let frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 49, CGRectGetWidth(self.view.frame), 49)
let tabBar: UITabBar = UITabBar(frame: frame)
tabBar.delegate = self // Need to import 'UITabBarDelegate'
self.view.addSubview(tabBar)

---------> Preparing TabbarItems

let tabBarItem1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let tabBarItem2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let tabBarItem3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let tabBarItem4 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)

---------> Adding UITabBarItems to UITabBar

tabBar.items = [tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4]
tabBar.selectedItem = tabBarItem1

---------> Receiving event in 'UITabBarDelegate'

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let selectedTag:Int = (tabBar.selectedItem?.tag)!
    print(selectedTag)
    switch selectedTag {
    case 0:
        print("tabBarItem1")
        //Do what ever you want here
    case 1:
        print("tabBarItem2")
        //Do what ever you want here
    case 2:
        print("tabBarItem3")
        //Do what ever you want here
    default:
        print("tabBarItem4")
        //Do what ever you want here
    }
}
like image 117
Ashok Avatar answered Sep 27 '22 22:09

Ashok