UINavigationBar and new iOS 5+ appearance API - how to supply two background images?

后端 未结 6 1984
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 00:50

I want to exploit the new iOS 5 appearance API to supply custom background images to all UINavigationBar instances in my app. To do this, it\'s as simple as this:

         


        
6条回答
  •  长发绾君心
    2021-01-05 01:03

    You can either set it globally with the class appearance proxy or set it on an instance of a navBar.

    I'm currently setting background on an instance of the nav bar and it seems to be working. I have two different navBars with different backgrounds. If you set it on an instance, you should be able to condition the code.

    UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myView];
    [viewControllers addObject:myNavController];
    
    // not supported on iOS4
    UINavigationBar *navBar = [myNavController navigationBar];
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        // right here, you could condition bg image based on properties of this instance
        // of the navBar or any other condition.
    
        [navBar setBackgroundImage:[UIImage imageNamed:@"bg.jpg"] forBarMetrics:UIBarMetricsDefault];
    }
    

    If you want to set using the class method, you can set for all:

    [[UINavigationBar appearance] setBackground ...
    

    I will admit though that it's pretty new and I'm just figuring it out like most folks.

提交回复
热议问题