Nikhil Lihla
Nikhil Lihla

Reputation: 613

how to set the image for tabbar item when tabbar controller supports all the orientations in iPhone

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];

Upvotes: 0

Views: 3763

Answers (1)

Kalpesh
Kalpesh

Reputation: 5334

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"] ;

}

}

Upvotes: 1

Related Questions