setBackgroundImage forBarMetrics + scaleToFill? - ios5

Since iOS5 you should set the backgroundimage of tool- or navigationbars via setBackgroundImage forBarMetrics.
In iOS4 I use:
#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
In iOS4 the image "scales to fill" in iOS5 the image is repeated if it's to small. Is there a way to set the behaviour to "scaleToFill" when you use setBackgroundImage forBarMetrics?
Thx!

Related

ios navigation bar backgroundimage

I have the following code that is supposed to set the background image on my navigation bar.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] )
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigation-bar-background"] forBarMetrics:UIBarMetricsDefault];
}
else
NSLog(#"WTF");
It works on the iPad but not on an iPhone, on the iPhone it logs WTF ..... same result without the if statement.
The iPhone I have is iPhone 4 running ios 6.0, the iPad is iPad 2 with ios 6.0
Any help is appreciated.
Thanks,
You could try this alternate old method
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navigation-bar-background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end

Change UINavigationBar background image to default - iOS 4.3

I change UINavigationBar background image with overloaded method
#implementation UINavigationBar (Background)
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"Header.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
But in some UIViewControllers I need to remove this background image. How I can do it?
You might look at this blog post from Sebastian Celis. It solves exactly the problem your facing.
And maybe also check this.
test in this way
+ (void)setNavigationbarBackground:(UINavigationBar *)nBar imageName:(NSString *)imgName {
if([nBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[nBar setBackgroundImage:[UIImage imageNamed:imgName] forBarMetrics:UIBarMetricsDefault];
} else {
[nBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:imgName]]];
}
}

How do I change the background of a UINavigationBar?

I'd like to change the background of my UINavigationBar to a [UIColor colorWithImage:], but it isn't working. What am I missing?
EDIT:
Once I've created my subclass, where do I set the UINavigationController to use it?
You can use the tintColor property to change the colour of a UINavigationBar, but to set an image as the background you'll have to provide your own UINavigationBar subclass and override the drawRect: method, for example:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: #"background-image.png"];
[img drawInRect:CGRectMake(0,
0,
self.frame.size.width,
self.frame.size.height)];
}
If you use Interface Builder to build your UI then to use the custom navigation bar, just select the UINavigationBar element in Interface Builder, open the Inspector and in the Identity tab specify your UINavigationBar subclass in the class field, like so:
To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as UINavigationBar+CustomBackground.m (it adds a custom category to UINavigationBar):
#implementation UINavigationBar (CustomBackground)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end

UIToolbar categories drawRect: won't work

I've tried the following:
#implementation UIToolbar (Image)
-(void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed: #"navigationBar.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Without any success, but it works perfectly with the exact same code for UINavigationBar+Image.
the drawRect method is not getting called for UIToolbar, why is that?
I found a solution
For UIToolBar, if you’re using it within a UINavigationController, you’ll want to also override drawLayer:inContext:, as this appears to be used instead of drawRect: when used within a navigation controller, for some weird reason.
Source: http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/
So I implemented - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx instead:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CALayer *imgLayer = [[CALayer alloc] init];
[imgLayer setContents:(id)[[UIImage imageNamed: #"toolbar.png"] CGImage]];
[imgLayer setBounds:CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height)];
[imgLayer setPosition:CGPointMake(self.bounds.size.width/2,self.bounds.size.height/2)];
[layer insertSublayer:imgLayer atIndex:0];
}

iPhone: UITabBar custom image doesn't work

I use the following UITTabBar and UINavigationBar built-in code to place my own custom image on the background of Tabbar and Navigation bar. It works great in my iPhone 3.0 application. Now, i am trying to convert my iPhone 3.0 application to iOS4.0. I ran the same project in Xcode 3.2.3(for iOS 4.0) now but unable to view my custom Tabbar image in the background of Tabbar. I tried debugging it, but it is not calling to this function UITabbar at all, but the same time it calls UINavigationBar well and works. Is this function(UITabbar) not there (or) can't use in iOS 4.0?
#implementation UITabBar(CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"Tabbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationImage.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Please advise.
Thank you.
I have resolved it by adding the code below:
[self.tabBarController.tabBar insertSubview:[ [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Tabbar.png"]] autorelease] atIndex:0];