Monday, 24 February 2014

iPhone Development:Create a UITabBarController programmatically in iPhone SDK




UITabbarController programmatically

Tags: #UITabBarController #programmatically #Tutorial #IOS8


Today, in this post i am going to tell you how we can create UITabBarController programmatically in objective C/ iPhone SDK.Though you can also chose a template for UITabBarController in the beginning of your project as the same is by default provided in Xcode by APPLE Inc.
In my development career i came across many scenarios in which i have to put/place UITabBarController  after two or three views/screens of my app. Some other questions that user put up on  various platform regarding UITabBarcontroller and one most common of them that i came across recently is how we can create a UITabBarController inside a UITabBarController and from that moment i made my mind of writing this tut.
Let's jump on to this
Select your viewController .h file( this the file where you want to create UITabBarController)
and create an instance of UITabBarController

    UITabBarController *tabbarController;

Now select your viewController.m and inside it's viewDidLoad method alloc and intialise it.

  tabbarController = [[UITabBarController alloc]init];

Now set tabBarcontroller delegate to self

  tabbarController.delegate=self;

Now we will create tabs or viwControllers for our tabs of UITabBarcontroller, first create an array called viewControllersArray and intialise it with capacity zero. We have to create an array because UItabBarController has an property called viewControllers which accepts an array.

Description of viewControllers prorperty as per documentation is
An array of the root view controllers displayed by the tab bar interface.
The default value of this property is nil. When configuring a tab bar controller, you can use this property to specify the content for each tab of the tab bar interface. The order of the view controllers in the array corresponds to the display order in the tab bar. Thus, the controller at index 0 corresponds to the left-most tab, the controller at index 1 the next tab to the right, and so on. If there are more view controllers than can fit in the tab bar, view controllers at the end of the array are managed by the More navigation controller, which is itself not included in this array.
If you change the value of this property at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. The tab bar items for the new view controllers are displayed immediately and are not animated into position. When changing the view controllers, the tab bar controller remembers the view controller object that was previously selected and attempts to reselect it. If the selected view controller is no longer present, it attempts to select the view controller at the same index in the array as the previous selection. If that index is invalid, it selects the view controller at index 0.


Now create your viewcontrollers as per your need one by one. If you need navigation for each viewController then you have to create separate navigationController for each viewController or tab.  .For demo purpose i am going to create only two viewControllers. After this we have to create an instance of UITaBarItem so that we can give our tab tittle,image etc., an lastly add the viewcontroller or navigationcontroller(if viewcontroller has navigation) into the viewControllersArray array.

HotSpotsViewController *hotSpotsVC = [[HotSpotsViewController alloc]initWithNibName:@"HotSpotsViewController" bundle:nil];
    UINavigationController *hotSpotsNavigationController = [[UINavigationController alloc]initWithRootViewController:hotSpotsVC];
    hotSpotsNavigationController.tabBarItem.title=@"HotSpots";
    UITabBarItem *item = [[UITabBarItem alloc] init];
    item.title=@"HotSpots";
    [item  setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor blackColor], UITextAttributeTextColor,
                                                       nil] forState:UIControlStateNormal];
     item. selectedImage=[UIImage imageNamed:@"HotSpotsSelected.png"] ;

item.image = [UIImage imageNamed:@"HotSpotsUnSelected.png"]];
    hotSpotsNavigationController.tabBarItem=item;
    hotSpotsNavigationController.navigationBar.hidden=YES;
    [viewControllersArray addObject:hotSpotsNavigationController];
    [item release];
    item = nil;
   
    SearchViewController *searchVC = [[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil];
    UINavigationController *searchNavigationController = [[UINavigationController alloc]initWithRootViewController:searchVC];
   
    UITabBarItem *itemSearch = [[UITabBarItem alloc] init];
    
 itemSearch. selectedImage=[UIImage imageNamed:@"SearchSelected.png"] ; 
itemSearch.image =[UIImage imageNamed:@"SearchUnSelected.png"]];
    itemSearch.title=@"Search";
    [itemSearch  setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                   [UIColor blackColor], UITextAttributeTextColor,
                                   nil] forState:UIControlStateNormal];
    searchNavigationController.tabBarItem=itemSearch;
    searchNavigationController.navigationBar.hidden=YES;
    [viewControllersArray addObject:searchNavigationController];
    [itemSearch release];
    itemSearch = nil;


Then we use viewControllers property of UITabBarController and assign our viewcontrollerArray to it and release our navigationController's and viewController's.
tabbarController.viewControllers=viewControllersArray;
 [viewControllersArray release];
    viewControllersArray = nil;
    [hotSpotsVC release];
    hotSpotsVC = nil;
    [searchVC release];
    searchVC = nil;
[hotSpotsNavigationController release];
    hotSpotsNavigationController = nil;
    [searchNavigationController release];
    searchNavigationController = nil;



Then we wrote lines to set our tab's background image and hide hover from tab and finally add tabBarController to our view as subview

 //hide hover
 [[UITabBar appearance] setSelectionIndicatorImage:[[[UIImage alloc]init]autorelease]];
 tabbarController.tabBar.backgroundImage=[UIImage imageNamed:@"btm_bar1.png"];
 [self.view addSubview:tabbarController.view];


Also we have to define UITabBarController delegate method to check for tab selection

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{
    if (tabBarController.selectedIndex==1) 

   {
        //second tab is selected
   }

}


Note:- If you are seeing a black line in tour tab for ios7 then you ca use below code to override it

1. if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
        tabbarController.tabBar.barStyle=UIBarStyleBlackOpaque;
    }


2. If tab image icons were not fitting up properly then you can use imageInset as shown below

    [reportItem setImageInsets:UIEdgeInsetsMake(6.5, 0, -6.5, 0)];



No comments:
Write comments