我想UIToolbar
在我的 iPhone 应用程序中设置背景图像。
目前我正在使用UIColor
's进行设置initWithPatternImage:
。
但它没有做我想要的。
请提出其他解决方案。
我想UIToolbar
在我的 iPhone 应用程序中设置背景图像。
目前我正在使用UIColor
's进行设置initWithPatternImage:
。
但它没有做我想要的。
请提出其他解决方案。
您不需要创建自定义类。您可以向 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
我已经测试过了,它运行良好。
PS:我昨晚用 4.3 进行了测试,它没有按预期工作,但在 4.2 上工作正常。我还没有找到任何原因,一旦找到正确的片段就会更新。
对于 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
限制的问题是,您需要更改您在所有 XIB 文件中拥有的每个 UIToolbar 对象的自定义类。而且我担心当您打开 UIImagePickerController 以匹配您的应用程序样式时它不会影响导航栏。
或者您可以制作自定义类并将其分配给您的控件(通过界面生成器)并在其中实现 drawRect 方法...
像
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"CustomImage.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
请参阅这个最近的问题:UINavigationBar 顶部的自定义徽标?