简体   繁体   English

在iphone中设置UIToolBar的背景图像

[英]setting background image of UIToolBar in iphone

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: . 目前我正在使用UIColorinitWithPatternImage:

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 您可以按如下方式向UIToolBar添加一个类别

@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. 我已经测试过它运行良好。

PS: I've tested with 4.3 last night & it didn't work as expected but works fine with 4.2. PS:我昨晚用4.3进行了测试,它没有达到预期的效果,但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. 对于4.3及更高版本,您应该不支持UIToolbar或UINavigationBar类别的子类。 The following code works with 4.3 以下代码适用于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. 与限制的问题是,你需要改变你的所有XIB文件中有充分的UIToolbar对象的customclass。 And I'm afraid that it will not affect navigation bar when you open UIImagePickerController to match your app style. 当你打开UIImagePickerController以匹配你的应用程序风格时,我担心它不会影响导航栏。

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

查看最近的这个问题: UINavigationBar顶部的自定义徽标?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM