Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom background image on UIToolbar in IOS5 SDK

Tags:

ios5

uitoolbar

Downloaded IOS5 SDK yesterday, and this code which I use to set my UIToolbar's background to a custom image stopped working. If I set the target to IOS4.3 and below it still works.

[self.bizToolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar-iphone.png"]] autorelease] atIndex:0];

Anybody encountered this yet on IOS 5?

like image 769
StackThis Avatar asked Jun 08 '11 18:06

StackThis


3 Answers

You can use: [[UIToolBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault]; (new in iOS 5)

like image 189
Simone Avatar answered Oct 23 '22 07:10

Simone


Suppose you linked iOS5 beta SDK, you could do something like this

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
} 

To realize this, take a look at here iOS 4.3 to iOS 5.0 API Differences and search for "UINavigationBar.h"

or take a close look at the new method signature here setBackgroundImage:forBarMetrics:

Also here is the UIBarMetrics enum type

Hope this helps.

like image 22
loretoparisi Avatar answered Oct 23 '22 08:10

loretoparisi


This worked on my toolbar:

//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];  

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    }
like image 31
RyeMAC3 Avatar answered Oct 23 '22 08:10

RyeMAC3