2013-02-04 21 views
1

我是IOS開發新手。我在動態設置Tabbar項目的圖像時遇到了一個問題。我已經分類了UITabbarItem。如何在tabbar控制器支持iPhone中的所有方向時設置tabbar項目的圖像

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

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

@end 

並添加我的tabbarcontroller到窗口。

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

我的Tabbar控制器完全支持所有的方向。但問題是,當我旋轉設備時,如何爲設備的當前方向設置標籤欄項目的圖像。該框架會自動設置爲每個選項卡,但我的圖像不會。我的應用程序中有四個選項卡。我通過網站搜索了很多,但無法獲得適當的解決方案。

編輯

這是我用於設置圖像的代碼。

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

請使用您所設定的圖像 – Rajneesh071

+0

檢查你的方位在所有視圖控制器 – Rajneesh071

回答

1

試試這個, 首先,你必須要兩個圖像即一個用於人像模式,另一個爲橫向模式。當您的設備更改方向時,應調用 - (void)willRotateToInterfaceOrientation方法。根據方向你可以設置tabbar項目圖像。

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

} 

} 
+0

感謝您的答覆粘貼代碼。但是這隻會爲當前控制器設置圖像。我需要爲所有4個選項卡設置它。我試過這個代碼,但它只會爲當前控制器設置它。 –

+0

UIViewController * view = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex]; – Kalpesh

+0

我試過了。但它似乎沒有幫助。不知道發生了什麼問題。 –