自定义 NavigationBar

话说自古武林剑法门派繁多,所以就有了每年9月9日的华山论剑。。。
iOS开发某些方面也是如此。拿自定义UINavigationBar这个很小的方面,也有N种方法,导致我在找寻答案的过程中走了很多弯路,多花了不少时间。现在就将这些方法做一下对比,谁优谁劣,留给读者思考:

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

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

1 [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];


2.松风剑法
此剑法中规中矩,能有不同变化。
可以随意改变bar的颜色,如下:
[代码]c#/cpp/oc代码:
1 [self.navigationController.navigationBar setTintColor:[UIColor orangeColor]];

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

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

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

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

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

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代码:

1 UINavigationBar *navBar = [myNavController navigationBar];
2 if ([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代码:

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代码:

1 @interface MyNavigationBar : UINavigationBar
2  
3 @end

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

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代码:

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代码:

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代码:

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代码:

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代码:

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。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值