iphoneiosuinavigationcontrollerthree20uinavigationbar

How to subclass UINavigationBar to work with Three20/iOS5?


Many of you have probably noticed that we cant anymore create a category to override UINavigationBar drawRect in iOS5. Now actually I don't see a good way of implementing this customization without using a subclass of UINavigationBar and set it inside a navigationController->navigationBar in my MainWindow.xib. However, TTNavigator do not recognize that navigationController since every TTViewController has their own superclass of TTNavigationController.


Solution

  • I don't know how to solve it for Three20 but in general for supporting IOS 5 you can use this method

    UIImage *image = [UIImage imageNamed:@"header.png"];
    
    if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
    } 
    else{
        UIImageView *imgView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [imgView setUserInteractionEnabled:NO];
        [imgView setTag:TOOLBAR_TAG];
        [navigationBar insertSubview:imgView atIndex:0];
    }