话说自古武林剑法门派繁多,所以就有了每年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 |
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)]; |
10 |
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 |
11 |
forBarMetrics:UIBarMetricsDefault]; |
12 |
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 |
13 |
forBarMetrics:UIBarMetricsLandscapePhone]; |
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], |
简单一点,就是下面的代码:
[代码]c#/cpp/oc代码:
1 |
UINavigationBar *navBar = [myNavController navigationBar]; |
2 |
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) |
5 |
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed: @"brnlthr_nav.jpg" ] forBarMetrics:UIBarMetricsDefault]; |
5.太极剑法
此剑法极简,主要靠内力,但是同样受到限制。
仅对iOS5之前版本有效,如下:
[代码]c#/cpp/oc代码:
1 |
@implementation UINavigationBar (CustomImage) |
2 |
- ( void )drawRect:(CGRect)rect { |
4 |
UIImage *image = [[UIImage imageNamed: @"header.png" ] retain]; |
5 |
[image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)]; |
6.雌雄双股剑法
将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。
7.两仪剑法
此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:
[代码]c#/cpp/oc代码:
1 |
@ interface MyNavigationBar : UINavigationBar |
[代码]c#/cpp/oc代码:
01 |
@implementation MyNavigationBar |
03 |
- ( void )drawRect:(CGRect)rect |
05 |
[super drawRect:rect]; |
09 |
@implementation UINavigationBar (LazyNavigationBar) |
11 |
return NSClassFromString( @"MyNavigationBar" ); |
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)]; |
8.金蛇剑法
此剑法劲道威猛,难以招架。
首先写CustomNaviBar类:
[代码]c#/cpp/oc代码:
1 |
@ interface CustomNaviBar : UINavigationBar |
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)]; |
然后创建xib文件,拖一个UINavigationController出来,将UINavigationController中的UINavigationBar替换为自定义的CustomNaviBar
然后,使用xib文件来创建UINavigationController:
[代码]c#/cpp/oc代码:
01 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
03 |
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
05 |
UINib *nib = [UINib nibWithNibName: @"Empty" bundle:nil]; |
06 |
UINavigationController* nc = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0]; |
07 |
self.window.rootViewController = nc; |
09 |
self.viewController = nil; |
11 |
[self.window makeKeyAndVisible]; |
此方法通吃所有iOS平台,有独步武林盟主的实力。。
9.独孤九剑
传说中的终极剑法,威力无比。
适用所有IOS版本。
首先,为appDelegate增加一个navigationController属性:
[代码]c#/cpp/oc代码:
1 |
@ interface DymAppDelegate : UIResponder <UIApplicationDelegate> |
3 |
UINavigationController *navController_; |
5 |
@property (strong, nonatomic) UIWindow *window; |
6 |
@property (strong, nonatomic) DymViewController *viewController; |
8 |
@property (nonatomic, readonly , retain) UINavigationController *navigationController; |
然后,将此设为rootViewController:
[代码]c#/cpp/oc代码:
01 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
03 |
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
05 |
self.viewController = [[[DymViewController alloc] initWithNibName: @"DymViewController" bundle:nil] autorelease]; |
07 |
self.window.rootViewController = self.navigationController; |
09 |
self.viewController = nil; |
11 |
[self.window makeKeyAndVisible]; |
下面是此剑法的心法:
[代码]c#/cpp/oc代码:
01 |
- (UINavigationController*)navigationController { |
02 |
if (navController_ == nil) { |
03 |
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; |
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]; |
12 |
[navController release]; |
15 |
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; |
16 |
[unarchiver setClass:[CustomNaviBar class ] |
17 |
forClassName:NSStringFromClass([UINavigationBar class ])]; |
18 |
navController_ = [[unarchiver decodeObjectForKey:kRootKey] retain]; |
23 |
return navController_; |
利用NSKeyedArchiver和NSKeyedUnarchiver来修改navigationbar。。。
独孤九剑一出,金蛇剑法也为之失色,实在是非常非常牛B的剑法啊,这次华山论剑,盟主之位就非他莫属了。。哈哈哈
=======================================
后记:虽然这次独孤九剑获得了天下第一剑的称号,但是世界之大,一定还有其他强大的剑法,在此抛砖引玉,以待明年华山再来争锋。。。
转载:http://www.devdiv.com/home.php?mod=space&uid=21666&do=blog&id=9081