0

我正在使用 UINavigation 控制器在 xCode 4.3.2 中创建一个 tabBar 应用程序。我在 AppDelegate 中使用以下代码

- (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  

但它根本不起作用。

4

1 回答 1

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
    }

谢谢

于 2012-06-15T04:57:07.410 回答