【问题标题】:setting background image of UIToolBar in iphone在iphone中设置UIToolBar的背景图片
【发布时间】:2010-04-17 08:55:48
【问题描述】:

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

目前我正在使用UIColorinitWithPatternImage:进行设置。

但它没有做我想做的事。

请提出其他解决方案。

【问题讨论】:

  • 如果 initWithPatternImage 没有做你想做的事,那么解释你想要的可能会增加你得到答案的机会。

标签: iphone objective-c uitoolbar uicolor


【解决方案1】:

您不需要创建自定义类。您可以向 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

我已经测试过了,效果很好。

P.S:我昨晚用 4.3 进行了测试,它没有按预期工作,但在 4.2 上工作正常。我还没有找到任何原因,找到合适的sn-p后会更新。

对于 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 对象的 customclass。而且我担心当您打开 UIImagePickerController 以匹配您的应用程序样式时它不会影响导航栏。

【讨论】:

  • 你应该调用 [super drawRect:rect];在您的 drawRect: 实施结束时。
【解决方案2】:

或者您可以创建自定义类并将其分配给您的控件(通过界面生成器)并在其中实现 drawRect 方法...

喜欢

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

【讨论】:

  • 是的,这是一个巧妙的隐藏功能,您还可以使用它来自定义导航控制器中的皮肤导航栏。
【解决方案3】:

查看这个最近的问题:Custom logo on top of UINavigationBar?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多