华山论剑 --- 自定义UINavigationBar背景方法汇总(我是转载收藏的呀)

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

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

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
 
2.松风剑法
此剑法中规中矩,能有不同变化。
可以随意改变bar的颜色,如下:

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

[self.navigationController.navigationBar setTintColor:[UIColor orangeColor]];
 
3.太极剑法
此剑法看似神奇,其实不厉害。
只能修改titleView,不能用于自定义背景,如下:

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

UIImage *logo = [UIImage imageNamed:@"title_bg"];
    [self.navigationItem setTitleView:[[[UIImageView alloc] initWithImage:logo] autorelease]];
如果你想用它修改背景一定会很失望的。。。
 
4.七星剑法
此剑法朴树迷离,闪瞎你dog眼,不过有种种限制,特定条件才能发挥威力。
只对iOS5有效。如下:

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

- (void)customizeAppearance
{
    // Create resizable images
    UIImage *gradientImage44 = [[UIImage imageNamed:@"surf_gradient_textured_44"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
 
    // Set the background image for *all* UINavigationBars
    [[UINavigationBar appearance] setBackgroundImage:gradientImage44 
        forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:gradientImage32 
        forBarMetrics:UIBarMetricsLandscapePhone];
 
    // Customize the title text for *all* UINavigationBars
    [[UINavigationBar appearance] setTitleTextAttributes:
        [NSDictionary dictionaryWithObjectsAndKeys:
            [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
            UITextAttributeTextColor, 
            [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
            UITextAttributeTextShadowColor, 
            [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
            UITextAttributeTextShadowOffset, 
            [UIFont fontWithName:@"Arial-Bold" size:0.0], 
            UITextAttributeFont, 
            nil]];
 
}
简单一点,就是下面的代码:

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

UINavigationBar *navBar = [myNavController navigationBar];
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    // set globablly for all UINavBars
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
  // ...
}
 
5.太极剑法
此剑法极简,主要靠内力,但是同样受到限制。
仅对iOS5之前版本有效,如下:

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

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    // Drawing code
    UIImage *image = [[UIImage imageNamed:@"header.png"] retain];
    [image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)];
    [image release];
    }
@end
 
6.雌雄双股剑法
将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。
 
7.两仪剑法
此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:

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

@interface MyNavigationBar : UINavigationBar

@end

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

@implementation MyNavigationBar

- (void)drawRect:(CGRect)rect 
{
    [super drawRect:rect];
}
@end

@implementation UINavigationBar (LazyNavigationBar)
+ (Class)class {
    return NSClassFromString(@"MyNavigationBar");
}

-(void)drawRect:(CGRect)rect {
    UIImage *backImage = [UIImage imageNamed:@"title_bg"];
[backImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];      
}
@end
 
8.金蛇剑法
此剑法劲道威猛,难以招架。
 
首先写CustomNaviBar类:

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

@interface CustomNaviBar : UINavigationBar
@end

@implementation CustomNaviBar
- (void)drawRect:(CGRect)rect {
	[[UIImage imageNamed:@"title_bg"] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
 
然后创建xib文件,拖一个UINavigationController出来,将UINavigationController中的UINavigationBar替换为自定义的CustomNaviBar
 
然后,使用xib文件来创建UINavigationController:

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    UINib *nib = [UINib nibWithNibName:@"Empty" bundle:nil];
    UINavigationController* nc =  [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];//
    self.window.rootViewController = nc;
    
    self.viewController = nil;
    
    [self.window makeKeyAndVisible];
    return YES;
}
 
此方法通吃所有iOS平台,有独步武林盟主的实力。。
 
9.独孤九剑
传说中的终极剑法,威力无比。
 
适用所有IOS版本。
 
首先,为appDelegate增加一个navigationController属性:

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

@interface DymAppDelegate : UIResponder <UIApplicationDelegate>
{
	UINavigationController *navController_;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) DymViewController *viewController;

@property (nonatomic, readonly, retain) UINavigationController *navigationController;
@end
 
然后,将此设为rootViewController:

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[[DymViewController alloc] initWithNibName:@"DymViewController" bundle:nil] autorelease];
    
    self.window.rootViewController = self.navigationController;
    
    self.viewController = nil;
    
    [self.window makeKeyAndVisible];
    return YES;
}
 
下面是此剑法的心法:

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

- (UINavigationController*)navigationController {
	if (navController_ == nil) {
		UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
		
		// Archive navigation controller for changing navigationbar class
		[navController navigationBar];
		NSMutableData *data = [[NSMutableData alloc] init];
		NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
		[archiver encodeObject:navController forKey:kRootKey];
		[archiver finishEncoding];
		[archiver release];
		[navController release];
		
		// Unarchive it with changing navigationbar class
		NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
		[unarchiver setClass:[CustomNaviBar class]
				forClassName:NSStringFromClass([UINavigationBar class])];
		navController_ = [[unarchiver decodeObjectForKey:kRootKey] retain];
		[unarchiver release];
		
		[data release];
	}
	return navController_;
}
利用NSKeyedArchiver和NSKeyedUnarchiver来修改navigationbar。。。
 
独孤九剑一出,金蛇剑法也为之失色,实在是非常非常牛B的剑法啊,这次华山论剑,盟主之位就非他莫属了。。哈哈哈
 
=======================================
后记:虽然这次独孤九剑获得了天下第一剑的称号,但是世界之大,一定还有其他强大的剑法,在此抛砖引玉,以待明年华山再来争锋。。。
 
 
posted @ 2013-04-02 10:08  这个世界欠你的太多  阅读(269)  评论(0编辑  收藏  举报