Custom background image on UIToolbar in IOS5 SDK
Asked Answered
R

5

26

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?

Read answered 8/6, 2011 at 18:34 Comment(1)
iOS5 is under NDA so I think you can discuss it on apple's dedicated forums onlyGlyoxaline
I
48

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

Inspissate answered 21/10, 2011 at 15:27 Comment(4)
This is brilliant, as it neatly sets the background for all your nav bars, you don't have to do it to each of them. Thanks for pointing me in the right direction.Paresh
Any equivalent for UIToolbar?Paresh
[[UIToolbar appearance] setBackgroundImage:barsBack forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]Paresh
How we can set same color pattern for back button hereCheroot
M
35

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.

Mona answered 14/6, 2011 at 12:51 Comment(0)
R
10

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]; 

    }
Regain answered 15/10, 2011 at 22:57 Comment(2)
There's no need to check for the system version (and this is generally not a good practice), because the -respondsToSelector check should be enough to determine whether to use the new way or old way of doing this.Indicator
ios5 working well but ios 4 is not responding. what should i do?Kopeck
G
3

This method is not documented and relies on specific subviews structure of UIToolbar which can be changed from version to version. So that exactly what probably happened with iOS5 release

P.S. If you check updated UIToolBar class reference you'll find another way to customize UIToolBar

Glyoxaline answered 8/6, 2011 at 18:38 Comment(1)
Validmir thanks for the pointer. I will check the documentation. I tried this for now and it works across all IOS versions. @interface WoodenToolbar : UIToolbar {} - (void)drawRect:(CGRect)rect; @end @implementation WoodenToolbar - (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed:@"navbar-iphone.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; } @endRead
C
0

This is along the same line as Simone's answer, but works for iOS 5 and iOS < 5. This is what I'm using in app. You need to call [UINavigationBar setupIos5PlusNavBarImage] somewhere in your app initialization (applicationDidFinishLaunching: is a good candidate). On iOS 5+, setupIos5PlusNavBarImage will use the new UIAppearance protocol to set the background and the drawRect override will be ignored. On iOS < 5, setupIos5PlusNavBarImage will basically be a no-op and the drawRect will handle drawing the image.

Interface:

@interface UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage;

- (void) drawRect: (CGRect) rect;

@end

Implementation:

@implementation UINavigationBar (CustomNavigationBar)

+ (void) setupIos5PlusNavBarImage
{
    if ([UINavigationBar respondsToSelector: @selector(appearance)])
    {
        [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"menuBar.png"] forBarMetrics: UIBarMetricsDefault];
    }
}

- (void) drawRect: (CGRect) rect
{
    UIImage* img = [UIImage imageNamed: @"menuBar.png"];
    [img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
Chronometer answered 30/11, 2011 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.