开发者

setting background image of UIToolBar in iphone

I want开发者_JS百科 to set a background image of UIToolbar in my iPhone application.

Currently I am setting by using the UIColor's initWithPatternImage:.

But it's not doing what I want.

Kindly suggest some other solution.


You don't need to create a custom class. You can add a catagory to UIToolBar as below

@implementation UIToolbar (UIToolbarCategory)
- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor colorWithRed:0.547 green:0.344 blue:0.118 alpha:1.000]; //whatever goes well with your background image.
    UIImage *img  = [UIImage imageNamed: @"your-navbar-img.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [img release];
    self.tintColor = color;
    [super drawRect:rect];
}
@end

I've tested and it is working well.

P.S: I've tested with 4.3 last night & it didn't work as expected but works fine with 4.2. I don't find any reasons yet, will update once I find right snippet.

For 4.3 and above, you should subclass the UIToolbar or UINavigationBar category addition is not supported. The following code works with 4.3

#import <UIKit/UIKit.h>


@interface MyAppToolBar : UIToolbar{

}

@end

@implementation MyAppToolBar

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    UIColor *color = [UIColor colorWithRed:0.547 green:0.344 blue:0.118 alpha:1.000]; //wood tan color
    UIImage *img  = [UIImage imageNamed: @"your-navbar-img.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [img release];
    self.tintColor = color;

}

@end

The problem with the limitation is, you need to change the customclass of every UIToolbar object you have in all of your XIB file. And I'm afraid that it will not affect navigation bar when you open UIImagePickerController to match your app style.


Or you can Make custom class and assign it to your control (through interface builder) and inside that implement drawRect method...

like

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


See this recent question: Custom logo on top of UINavigationBar?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜