2012-06-15 62 views
0

我正在用UINavigation控制器在xCode 4.3.2中創建一個tabBar應用程序。我在AppDelegate中使用下面的代碼Xcode 4.3.2 UINavigationBar圖像背景

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 
    self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects: 
             [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease], viewController2, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

現在的問題是,我想要在導航欄中的自定義背景圖像。我發現的解決方案是編寫UINavigationBar的子類,並在界面構建器中設置新的子類。但在我的情況下,我設置導航控制器編程,然後如何實現這一目標? 我也嘗試創建類別爲

@implementation UINavigationBar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

但它並沒有工作。

+0

你想設置的TabBar或導航欄的背景圖像。你的問題標題是相關的導航欄,你張貼代碼爲tabbar? –

+0

Mitesh Khatri,我想設置navigationBar的背景圖片,我發佈了代碼以顯示我如何添加導航控制器。我試圖subclass navigationBar,但我不知道如何設置navigatioBar類到我的新子類,因爲我正在做所有的編程。 Thanx等待你的回覆 –

+0

你可以看到我的帖子http://stackoverflow.com/questions/9386633/cant-set-a-background-image-to-uinavigationcontroller/17105203#17105203 –

回答

6

對於導航欄使用:

UIImage *image = [UIImage imageNamed: @"NavBarImage.png"]; 
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];` 

and for Tabbar: 

    // not supported on iOS4  
    UITabBar *tabBar = [tabController tabBar]; 
    if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) 
    { 
     // set it just for this instance 
     [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]]; 

     // set for all 
     // [[UITabBar appearance] setBackgroundImage: ... 
    } 
    else 
    { 
     // ios 4 code here 
    } 

感謝

+0

Thanx Mitesh Khatri,但我期待爲後衛兼容的解決方案,至少適用於iOS 4。 –