设置iphone UIToolBar的背景图片

时间:2021-09-27 22:43:53

I want to set a background image of UIToolbar in my iPhone application.

我想在我的iPhone应用程序中设置一个UIToolbar的背景图像。

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

目前我正在使用UIColor的initWithPatternImage:进行设置。

But it's not doing what I want.

但这并不是我想要的。

Kindly suggest some other solution.

请提出其他解决方案。

3 个解决方案

#1


3  

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

或者,您可以创建自定义类并将其分配给您的控件(通过接口构建器),并在其内部实现drawRect方法……

like

就像

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

#2


4  

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

您不需要创建自定义类。您可以向UIToolBar添加一个catagory,如下所示

@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.

P。我昨天晚上用4.3测试过了,它没有像预期的那样工作,但是用4.2就可以了。我还没有找到任何原因,一旦找到正确的片段就会更新。

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

对于4.3及以上,不支持UIToolbar或UINavigationBar类别添加。以下代码适用于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.

限制的问题是,您需要更改所有XIB文件中所有UIToolbar对象的customclass。我担心当你打开UIImagePickerController以匹配你的应用风格时它不会影响导航栏。

#3


1  

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

看到最近的问题:UINavigationBar顶部的自定义标志了吗?

#1


3  

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

或者,您可以创建自定义类并将其分配给您的控件(通过接口构建器),并在其内部实现drawRect方法……

like

就像

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

#2


4  

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

您不需要创建自定义类。您可以向UIToolBar添加一个catagory,如下所示

@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.

P。我昨天晚上用4.3测试过了,它没有像预期的那样工作,但是用4.2就可以了。我还没有找到任何原因,一旦找到正确的片段就会更新。

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

对于4.3及以上,不支持UIToolbar或UINavigationBar类别添加。以下代码适用于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.

限制的问题是,您需要更改所有XIB文件中所有UIToolbar对象的customclass。我担心当你打开UIImagePickerController以匹配你的应用风格时它不会影响导航栏。

#3


1  

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

看到最近的问题:UINavigationBar顶部的自定义标志了吗?