0

I am new to IOS development. I am facing an issue in dynamically setting the images for Tabbar items. I have sub-classed the UITabbarItem .

@interface CustomTabBarItem : UITabBarItem
{
    UIImage *customHighlightedImage;
    UIImage *customStdImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;
@property (nonatomic, retain) UIImage *customStdImage;

@end 

And added my tabbarcontroller to the window.

[_tabBarController setViewControllers: [[NSArray alloc] initWithObjects:1,2,3,4,nil] animated: YES];
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;
_tabBarController.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbarBackground.png"]];
[self.window addSubview:_tabBarController.view];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];

My Tabbar controller is perfectly supporting all the orientation. But the problem is when i rotate the device, how can I set the images of tabbar items for the current orientation of the device. The frame gets automatically set for each tab but my images does not. I have four tabs in my application. I have searched a lot through the site but was not able to get the appropriate solution.

EDIT

This is the code that i am using for setting the images.

 tabBarItemBrowse=[[CustomTabBarItem alloc] init];
    tabBarItemBrowse.imageInsets=UIEdgeInsetsMake(6, 0, -6, 0);
    //    tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser.png"];
    //    tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act.png"];
    if ( [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait ||  [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser.png"];
        tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act.png"];
    }

    else
    {
        tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser568.png"];
        tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act568.png"];

    }
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    BrowseNav  = [[CustomNavigationControllerViewController alloc] initWithRootViewController:self.viewController];
    BrowseNav.tabBarItem = tabBarItemBrowse;
    BrowseNav.navigationBar.tintColor = [UIColor blackColor];
    BrowseNav.navigationBarHidden=YES;
    [tabBarItemBrowse release];
Nikhil Lihla
  • 607
  • 6
  • 21

1 Answers1

1

Try this, Firstly ,you have to take two images i.e one for portrait mode and another for landscape mode . When your device change the orientation the -(void)willRotateToInterfaceOrientation method should be call. According to orientation you can set tabbar item image.

   -(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration: (NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
{

  yourViewController.tabBarItem.image = [UIImage imageNamed:@"portrait.png"] ;
}
 else

{

  yourViewController.tabBarItem.image = [UIImage imageNamed:@"Landscape.png"] ;

}

}
Kalpesh
  • 5,336
  • 26
  • 41
  • thanks for the reply. But this would only set the image for the current controller. and i need to set it for all 4 tabs. i tried this code but it would only set it for the current controller. – Nikhil Lihla Feb 04 '13 at 09:40
  • UIViewController *view= [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex]; – Kalpesh Feb 04 '13 at 10:02
  • i tried it. but it doesn't seems to be helpful. Don't know what is going wrong. – Nikhil Lihla Feb 04 '13 at 10:55
  • thanks man!! now it works with little modification that was to be done according to the code in my app. thank you very much. You saved my day. – Nikhil Lihla Feb 04 '13 at 11:42