0

I am creating a tabBar application in xCode 4.3.2 with UINavigation controller. I am using following code in 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; } 

Now problem is that i want custom background image in navigation bar. The solutions i have found are writing subclass of UINavigationBar and set new subclass in interface builder. But in my case i am setting navigation controller programmatically then how to achieve this? I have also tried creating category as

@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 

But its not working at all.

3
  • You want to set Background image of Tabbar or Navigation Bar. Your question heading is releated to navigation bar and you posted code for tabbar??? Commented Jun 15, 2012 at 4:55
  • Mitesh Khatri, i want to set background image of navigationBar, i posted code to show how i am adding navigation controller. I tried to subclass navigationBar, but i don't know how to set navigatioBar class to my new subclass as i am doing all programmatically. Thanx waiting for your reply Commented Jun 15, 2012 at 6:45
  • You can see my post at stackoverflow.com/questions/9386633/… Commented Jun 14, 2013 at 9:24

1 Answer 1

4

For Navigationbar use this:

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 } 

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx Mitesh Khatri, but i am looking for backword compatible solution, atleast for iOS 4.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.