华山论剑 --- 自定义UINavigationBar背景方法汇总

来源:互联网 发布:软件涉密资质 编辑:程序博客网 时间:2024/04/28 13:59
http://blog.sina.com.cn/s/blog_6cffce770100wvgf.html
话说自古武林剑法门派繁多,所以就有了每年9月9日的华山论剑。。。
iOS开发某些方面也是如此。拿自定义UINavigationBar这个很小的方面,也有N种方法,导致我在找寻答案的过程中走了很多弯路,多花了不少时间。现在就将这些方法做一下对比,谁优谁劣,留给读者思考:

1.辟邪剑法
此剑法非常初级
只能实现系统定义的样式。如下:

[代码]c#/cpp/oc代码:

view source
print?
1[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];


2.松风剑法
此剑法中规中矩,能有不同变化。
可以随意改变bar的颜色,如下:

[代码]c#/cpp/oc代码:

view source
print?
1[self.navigationController.navigationBar setTintColor:[UIColor orangeColor]];

3.太极剑法
此剑法看似神奇,其实不厉害。
只能修改titleView,不能用于自定义背景,如下:

[代码]c#/cpp/oc代码:

view source
print?
1UIImage *logo = [UIImage imageNamed:@"title_bg"];
2    [self.navigationItem setTitleView:[[[UIImageView alloc] initWithImage:logo] autorelease]];
如果你想用它修改背景一定会很失望的。。。

4.七星剑法
此剑法朴树迷离,闪瞎你dog眼,不过有种种限制,特定条件才能发挥威力。
只对iOS5有效。如下:

[代码]c#/cpp/oc代码:

view source
print?
01- (void)customizeAppearance
02{
03    // Create resizable images
04    UIImage *gradientImage44 = [[UIImage imageNamed:@"surf_gradient_textured_44"
05        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
06    UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"
07        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
08   
09    // Set the background image for *all* UINavigationBars
10    [[UINavigationBar appearance] setBackgroundImage:gradientImage44 
11        forBarMetrics:UIBarMetricsDefault];
12    [[UINavigationBar appearance] setBackgroundImage:gradientImage32 
13        forBarMetrics:UIBarMetricsLandscapePhone];
14   
15    // Customize the title text for *all* UINavigationBars
16    [[UINavigationBar appearance] setTitleTextAttributes:
17        [NSDictionary dictionaryWithObjectsAndKeys:
18            [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
19            UITextAttributeTextColor, 
20            [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
21            UITextAttributeTextShadowColor, 
22            [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
23            UITextAttributeTextShadowOffset, 
24            [UIFont fontWithName:@"Arial-Bold" size:0.0], 
25            UITextAttributeFont, 
26            nil]];
27   
28}<BR>

简单一点,就是下面的代码:

[代码]c#/cpp/oc代码:

view source
print?
1UINavigationBar *navBar = [myNavController navigationBar];
2if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
3{
4    // set globablly for all UINavBars
5    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
6  // ...
7}

5.太极剑法
此剑法极简,主要靠内力,但是同样受到限制。
仅对iOS5之前版本有效,如下:

[代码]c#/cpp/oc代码:

view source
print?
1@implementation UINavigationBar (CustomImage)
2- (void)drawRect:(CGRect)rect {
3    // Drawing code
4    UIImage *image = [[UIImage imageNamed:@"header.png"] retain];
5    [image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)];
6    [image release];
7    }
8@end

6.雌雄双股剑法
将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。

7.两仪剑法
此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:

[代码]c#/cpp/oc代码:

view source
print?
1@interface MyNavigationBar : UINavigationBar
2  
3@end

[代码]c#/cpp/oc代码:

view source
print?
01@implementation MyNavigationBar
02  
03- (void)drawRect:(CGRect)rect 
04{
05    [super drawRect:rect];
06}
07@end
08  
09@implementation UINavigationBar (LazyNavigationBar)
10+ (Class)class {
11    return NSClassFromString(@"MyNavigationBar");
12}
13  
14-(void)drawRect:(CGRect)rect {
15    UIImage *backImage = [UIImage imageNamed:@"title_bg"];
16[backImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];      
17}
18@end


8.金蛇剑法
此剑法劲道威猛,难以招架。

首先写CustomNaviBar类:

[代码]c#/cpp/oc代码:

view source
print?
1@interface CustomNaviBar : UINavigationBar
2@end
3  
4@implementation CustomNaviBar
5- (void)drawRect:(CGRect)rect {
6    [[UIImage imageNamed:@"title_bg"] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
7}
8@end


然后创建xib文件,拖一个UINavigationController出来,将UINavigationController中的UINavigationBar替换为自定义的CustomNaviBar

然后,使用xib文件来创建UINavigationController:

[代码]c#/cpp/oc代码:

view source
print?
01- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
02{
03    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
04      
05    UINib *nib = [UINib nibWithNibName:@"Empty" bundle:nil];
06    UINavigationController* nc =  [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];//
07    self.window.rootViewController = nc;
08      
09    self.viewController = nil;
10      
11    [self.window makeKeyAndVisible];
12    return YES;
13}


此方法通吃所有iOS平台,有独步武林盟主的实力。。

9.独孤九剑
传说中的终极剑法,威力无比。

适用所有IOS版本。

首先,为appDelegate增加一个navigationController属性:

[代码]c#/cpp/oc代码:

view source
print?
1@interface DymAppDelegate : UIResponder <UIApplicationDelegate>
2{
3    UINavigationController *navController_;
4}
5@property (strong, nonatomic) UIWindow *window;
6@property (strong, nonatomic) DymViewController *viewController;
7  
8@property (nonatomic, readonly, retain) UINavigationController *navigationController;
9@end


然后,将此设为rootViewController:

[代码]c#/cpp/oc代码:

view source
print?
01- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
02{
03    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
04      
05    self.viewController = [[[DymViewController alloc] initWithNibName:@"DymViewController" bundle:nil] autorelease];
06      
07    self.window.rootViewController = self.navigationController;
08      
09    self.viewController = nil;
10      
11    [self.window makeKeyAndVisible];
12    return YES;
13}


下面是此剑法的心法:

[代码]c#/cpp/oc代码:

view source
print?
01- (UINavigationController*)navigationController {
02    if (navController_ == nil) {
03        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
04          
05        // Archive navigation controller for changing navigationbar class
06        [navController navigationBar];
07        NSMutableData *data = [[NSMutableData alloc] init];
08        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
09        [archiver encodeObject:navController forKey:kRootKey];
10        [archiver finishEncoding];
11        [archiver release];
12        [navController release];
13          
14        // Unarchive it with changing navigationbar class
15        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
16        [unarchiver setClass:[CustomNaviBar class]
17                forClassName:NSStringFromClass([UINavigationBar class])];
18        navController_ = [[unarchiver decodeObjectForKey:kRootKey] retain];
19        [unarchiver release];
20          
21        [data release];
22    }
23    return navController_;
24}

利用NSKeyedArchiver和NSKeyedUnarchiver来修改navigationbar。。。

独孤九剑一出,金蛇剑法也为之失色,实在是非常非常牛B的剑法啊,这次华山论剑,盟主之位就非他莫属了。。哈哈哈

=======================================
后记:虽然这次独孤九剑获得了天下第一剑的称号,但是世界之大,一定还有其他强大的剑法,在此抛砖引玉,以待明年华山再来争锋。。。


0 0