16

我有一个 iPhone 应用程序正在使用UINavigationController并希望使用自定义背景图像自定义元素。UINavigationController's UINavigationBar我可以很容易地使用 Objective-C 类别来做到这一点,如下所示:

http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html

我想对 做同样的事情UINavigationController's UIToolbar,但同样的方法似乎不起作用(尽管我完全不知道为什么不这样做。)我环顾四周,人们似乎建议子类化UIToolbar,但这是不可能的对于UINavigationController's工具栏,它是只读的UIToolbar. 我想使用UINavigationController's工具栏而不是仅仅制作子视图工具栏,因为我正在使用滑入式setToolbarHidden动画。

任何人都知道是否可以将背景图像应用于此UINavigationController工具栏(很可能通过某种方式覆盖该drawRect方法)?

4

4 回答 4

10

更新:

在 iOS 5.0+ 中,您现在可以使用UIAppearance自定义导航栏。


另一种方法是简单地为应用程序的UINavigationBar类创建一个类别。这很容易实现:

界面:

@interface UINavigationBar (TENavigationBar)

- (void) drawRect:(CGRect)rect;

@end

执行:

@implementation UINavigationBar (TENavigationBar)

- (void) drawRect:(CGRect)rect
{
    UIImage *image = [UIImage imageNamed:@"navigation_background"];

    [image drawInRect:CGRectMake(0, 
                                 0, 
                                 self.frame.size.width, 
                                 self.frame.size.height)];
}

@end

然后这将自动全局应用于您的所有UINavigationBar'。

于 2010-10-31T18:52:25.230 回答
9

您需要子类化而不是创建类别。

我不太确定为什么一个类别适用于UINavigationBar但不UIToolbar适用,但子类化适用于它们两者,我认为这是自定义此类内容的更标准方式。

因此,要创建子类UIToolbar,创建一个名为MyToolbar(或类似的)的类并将其放在 .h 中:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MyToolbar : UIToolbar {}

- (void)drawRect:(CGRect)rect;

@end

这在 .m 文件中:

#import "MyToolbar.h"

@implementation MyToolbar

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

@end

从那里你必须告诉导航控制器使用MyToolbar而不是UIToolbar. 我发现最简单的方法是在 Interface Builder 中选择您的 Navigation Controller,然后在 Attributes Inspector 中选中“Shows Toolbar”框。工具栏应显示在NavigationController窗口中。单击它,然后在 Identity Inspector 中将类更改为MyToolbar.

于 2010-07-24T03:03:16.760 回答
2

最好的解决方案之一是创建 UINavigationBar 和 UIToolbar 的类别。接下来的课程为您提供了 iOS 4.0 和 iOS 5.0 兼容性的解决方案:

唯一的事情是您需要在 viewDidLoad 中调用下一个方法,例如:

// Customizacion de navigation bar y toolbar compatible con iOS4 e iOS5
[UINavigationBar iOS5UINavigationBarBackgroundImage];
[UIToolbar iOS5UIToolbarBackgroundImage];    

因此,适用于 iOS 4.0 和 iOS 5.0 兼容性的类 Category 用于自定义 BackgroundImage 结果如下:

@implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect 
{    
    UIImage* img = [UIImage imageNamed: @"navigation-bg.png"];
    [img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
    [self setTintColor:UIColorFromRGB(0x5ca666)];      
}
+ (void) iOS5UINavigationBarBackgroundImage
{
    if ([UINavigationBar respondsToSelector: @selector(appearance)])
    {
        [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"navigation-bg.png"] forBarMetrics: UIBarMetricsDefault];
        [[UINavigationBar appearance] setTintColor:UIColorFromRGB(0x5ca666)];
    }
}
@end

@implementation UIToolbar (BackgroundImage)
- (void)drawRect:(CGRect)rect 
{
    UIImage *image = [UIImage imageNamed: @"toolbar-bg.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self setTintColor:UIColorFromRGB(0x5ca666)];
}
+ (void) iOS5UIToolbarBackgroundImage
{
    if ([UIToolbar respondsToSelector: @selector(appearance)])
    {
        [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed: @"toolbar-bg.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
        [[UIToolbar appearance] setTintColor:UIColorFromRGB(0x5ca666)];
    }
}
@end

问候!

于 2012-06-08T10:28:47.553 回答
0

一个简单的选择是继承 UINavigationBar/UIToolbar 并使用以下方法初始化导航控制器:

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass

但是,对于简单的自定义(如背景图像、阴影等),我建议使用UIAppearance协议模式。

于 2013-02-21T02:47:10.083 回答