自定义 NavigationBar
2016-03-21 22:46
411 查看
话说自古武林剑法门派繁多,所以就有了每年9月9日的华山论剑。。。
iOS开发某些方面也是如此。拿自定义UINavigationBar这个很小的方面,也有N种方法,导致我在找寻答案的过程中走了很多弯路,多花了不少时间。现在就将这些方法做一下对比,谁优谁劣,留给读者思考:
1.辟邪剑法
此剑法非常初级
只能实现系统定义的样式。如下:
2.松风剑法
此剑法中规中矩,能有不同变化。
可以随意改变bar的颜色,如下:
[代码]c#/cpp/oc代码:
3.太极剑法
此剑法看似神奇,其实不厉害。
只能修改titleView,不能用于自定义背景,如下:
如果你想用它修改背景一定会很失望的。。。
4.七星剑法
此剑法朴树迷离,闪瞎你dog眼,不过有种种限制,特定条件才能发挥威力。
只对iOS5有效。如下:
简单一点,就是下面的代码:
5.太极剑法
此剑法极简,主要靠内力,但是同样受到限制。
仅对iOS5之前版本有效,如下:
6.雌雄双股剑法
将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。
7.两仪剑法
此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:
8.金蛇剑法
此剑法劲道威猛,难以招架。
首先写CustomNaviBar类:
然后创建xib文件,拖一个UINavigationController出来,将UINavigationController中的UINavigationBar替换为自定义的CustomNaviBar
然后,使用xib文件来创建UINavigationController:
此方法通吃所有iOS平台,有独步武林盟主的实力。。
9.独孤九剑
传说中的终极剑法,威力无比。
适用所有IOS版本。
首先,为appDelegate增加一个navigationController属性:
然后,将此设为rootViewController:
下面是此剑法的心法:
利用NSKeyedArchiver和NSKeyedUnarchiver来修改navigationbar。。。
iOS开发某些方面也是如此。拿自定义UINavigationBar这个很小的方面,也有N种方法,导致我在找寻答案的过程中走了很多弯路,多花了不少时间。现在就将这些方法做一下对比,谁优谁劣,留给读者思考:
1.辟邪剑法
此剑法非常初级
只能实现系统定义的样式。如下:
[代码]c#/cpp/oc代码:
1 | [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque]; |
此剑法中规中矩,能有不同变化。
可以随意改变bar的颜色,如下:
[代码]c#/cpp/oc代码:
1 | [self.navigationController.navigationBar setTintColor:[UIColororangeColor]]; |
此剑法看似神奇,其实不厉害。
只能修改titleView,不能用于自定义背景,如下:
[代码]c#/cpp/oc代码:
1 | UIImage *logo=[UIImageimageNamed: @"title_bg" ]; |
2 | [self.navigationItem setTitleView:[[[UIImageViewalloc]initWithImage:logo]autorelease]]; |
4.七星剑法
此剑法朴树迷离,闪瞎你dog眼,不过有种种限制,特定条件才能发挥威力。
只对iOS5有效。如下:
[代码]c#/cpp/oc代码:
01 | - ( void )customizeAppearance |
02 | { |
03 | // Createresizableimages |
04 | UIImage *gradientImage44=[[UIImageimageNamed: @"surf_gradient_textured_44" ] |
05 | resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0,0)]; |
06 | UIImage *gradientImage32=[[UIImageimageNamed: @"surf_gradient_textured_32" ] |
07 | resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0,0)]; |
08 |
09 | // |
10 | [[UINavigationBar |
11 | forBarMetrics:UIBarMetricsDefault]; |
12 | [[UINavigationBar |
13 | forBarMetrics:UIBarMetricsLandscapePhone]; |
14 |
15 | // |
16 | [[UINavigationBar |
17 | [NSDictionary dictionaryWithObjectsAndKeys: |
18 | [UIColor |
19 | UITextAttributeTextColor, |
20 | [UIColor |
21 | UITextAttributeTextShadowColor, |
22 | [NSValue |
23 | UITextAttributeTextShadowOffset, |
24 | [UIFont fontWithName: @"Arial-Bold" size:0.0], |
25 | UITextAttributeFont, |
26 | nil]]; |
27 |
28 | }<br> |
[代码]c#/cpp/oc代码:
1 | UINavigationBar |
2 | if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) |
3 | { |
4 | // |
5 | [[UINavigationBar @"brnlthr_nav.jpg" ] forBarMetrics:UIBarMetricsDefault]; |
6 | // ... |
7 | } |
此剑法极简,主要靠内力,但是同样受到限制。
仅对iOS5之前版本有效,如下:
[代码]c#/cpp/oc代码:
1 | @implementation |
2 | - ( void )drawRect:(CGRect)rect { |
3 | // |
4 | UIImage @"header.png" ] retain]; |
5 | [image |
6 | [image release]; |
7 | } |
8 | @end |
将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。
7.两仪剑法
此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:
[代码]c#/cpp/oc代码:
1 | @ interface MyNavigationBar |
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 |
10 | + (Class) class { |
11 | return NSClassFromString( @"MyNavigationBar" ); |
12 | } |
13 |
14 | -( void )drawRect:(CGRect)rect { |
15 | UIImage @"title_bg" ]; |
16 | [backImage |
17 | } |
18 | @end |
此剑法劲道威猛,难以招架。
首先写CustomNaviBar类:
[代码]c#/cpp/oc代码:
1 | @ interface CustomNaviBar |
2 | @end |
3 |
4 | @implementation CustomNaviBar |
5 | - ( void )drawRect:(CGRect)rect { |
6 | [[UIImage imageNamed: @"title_bg" ] |
7 | } |
8 | @end |
然后,使用xib文件来创建UINavigationController:
[代码]c#/cpp/oc代码:
01 | - (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions |
02 | { |
03 | self.window |
04 |
05 | UINib @"Empty" bundle:nil]; |
06 | UINavigationController* // |
07 | self.window.rootViewController |
08 |
09 | self.viewController |
10 |
11 | [self.window makeKeyAndVisible]; |
12 | return YES; |
13 | } |
9.独孤九剑
传说中的终极剑法,威力无比。
适用所有IOS版本。
首先,为appDelegate增加一个navigationController属性:
[代码]c#/cpp/oc代码:
1 | @ interface DymAppDelegate |
2 | { |
3 | UINavigationController *navController_; |
4 | } |
5 | @property |
6 | @property |
7 |
8 | @property readonly , |
9 | @end |
[代码]c#/cpp/oc代码:
01 | - (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions |
02 | { |
03 | self.window |
04 |
05 | self.viewController @"DymViewController" bundle:nil] autorelease]; |
06 |
07 | self.window.rootViewController |
08 |
09 | self.viewController |
10 |
11 | [self.window makeKeyAndVisible]; |
12 | return YES; |
13 | } |
[代码]c#/cpp/oc代码:
01 | - (UINavigationController*)navigationController{ |
02 | if (navController_ |
03 | UINavigationController |
04 |
05 | // |
06 | [navController navigationBar]; |
07 | NSMutableData |
08 | NSKeyedArchiver |
09 | [archiver |
10 | [archiver finishEncoding]; |
11 | [archiver release]; |
12 | [navController release]; |
13 |
14 | // |
15 | NSKeyedUnarchiver |
16 | [unarchiver class ] |
17 | forClassName:NSStringFromClass([UINavigationBar class ])]; |
18 | navController_ |
19 | [unarchiver release]; |
20 |
21 | [data release]; |
22 | } |
23 | return navController_; |
24 | } |
相关文章推荐
- 文件与目录(权限,操作,搜索)
- Word2vec的词聚类结果与LDA的主题词聚类结果,有什么不同?
- 安卓文件写入与读取
- Codeforces Beta Round #5 B. Center Alignment 模拟题
- linux IPC总结——管道
- (1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”; (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- 《软件工程》前三章读后感
- Servlet和jsp小应用
- 常用的Xcode插件
- python系列之 RabbitMQ -- TOPICS
- Impala 1、Impala理论
- Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍
- Problem D: 最小节点(线性表)
- CreateWindowEx()自定义窗口句柄,消息处理
- 构建之法:1、2、3章阅读后感
- Android之性能优化
- HDU 5365 Run [Ad Hoc]
- ActionMode使用及样式配置
- Spring和MyBatis环境整合
- 关于MVC中利用Javascript跳转页面的问题的备忘