Pages

Showing posts with label UITabbar. Show all posts
Showing posts with label UITabbar. Show all posts

Sunday, September 18, 2011

Add and Remove Tabbar item Run Time


In ios sdk, some time we need to insert/delete tab bar item run time. So In this case, we can add and remove tab bar items by following below code.


// Remove and Add Tab bar item from tabbar controller at run time:

-(IBAction) RemoveTab:(id)sender
{
               UIViewController *itemToRemove = nil;
   
        NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
        for (UIViewController *aView in newItems)
        {
                       if ([[[aView class] description] isEqualToString:@"FirstViewController"])
            {
                //store this item to remove later
                         itemToRemove = aView;
            }
        }
        if (itemToRemove)
        {
            [newItems removeObject:itemToRemove];
              
        }
}


// Add  Tab bar item in tabbar controller at run time:

-(IBAction) AddTab:(id)sender
{
              
                   NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
                       
//Add New ViewController

                   UITabBarItem *obj = [[FirstViewController alloc] init];
// or use below
               //UITabBarItem *obj = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

                   [newItems addObject:obj];
                        obj.title = @"First";
                        [obj release];
              
                        [self.tabBarController setViewControllers:newItems];

}


This code has worked perfectly for me, but if you have any problem inform me.
Or get any new or more efficient way.


Thanks……………..

Sunday, June 5, 2011

To change the default color of the Tab-Bar


Add the below code in viewWillAppear of your class
CGRect frame =
CGRectMake(0.0, 0.0,self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.6 alpha:0.8]];
[v setAlpha:0.5];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
[v release];

And we can also image pattern color.
 UIImage *i = [UIImage imageNamed:@"abc.png"];
 UIColor *c = [[UIColor alloc] initWithPatternImage:i];

And we can also place image in tabbar background.
CGRect frame =
CGRectMake(0.0, 0.0,self.view.bounds.size.width, 48);
UIImageView *v = [[UIImageView alloc] initWithFrame:frame];
[v setImage:[UIImage imageNamed:@”YOUR IMAGE NAME”]];
[v setAlpha:0.5];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
[v release];

Saturday, May 28, 2011

Iphone:set badge on tabbar Item

UITabBarItem *tbi = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:2];
or
UITabBarItem *tbi = (UITabBarItem *)[AppDel.tabBarController.tabBar.items objectAtIndex:2];

tbi.badgeValue = @"2"; // 2 define the value that would be display on tabbar item.


Saturday, April 16, 2011

How to hide/show Tab bar animatingly with iphone sdk:


Here we are using UIView Animation and set frame to hide and show the tab bar.
BOOL hiddenTabBar;
- (void) hidetabbar {
           
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.5];
           
            for(UIView *view in self.tabBarController.view.subviews)
            {
                   NSLog(@"%@", view);
                  
                   if([view isKindOfClass:[UITabBar class]])
                   {
                            
                             if (hiddenTabBar) {
                                      [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
                             } else {
                                      [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
                             }
                   } else {
                             if (hiddenTabBar) {
                                      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
                             } else {
                                      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
                             }
                            
                   }
            }
           
            [UIView commitAnimations];       
            hiddenTabBar = !hiddenTabBar;
}

Happy programming................