码迷,mamicode.com
首页 > 移动开发 > 详细

iOS每个ViewController一个NavigationBar

时间:2016-11-22 09:37:56      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:item   vcpu   ati   imp   ram   btn   finish   ima   stat   

iOS每个ViewController一个NavigationBar

 

在日常开发中,我们经常会碰到一些关于导航栏的问题,例如视觉设计,经常性的改变NavigationBar的风格,虽然我们能够在viewwillApper中来进行处理,但是总是太麻烦,而且需要写很多多余的代码,今天就来讲讲这种效果,其实已经有很多APP都是使用这种效果

我们先来看看已经有的一些APP使用的这种效果

技术分享

这是天猫APP的效果,注意观察他的导航栏

 

 

 

这是网易新闻,注意看导航栏

技术分享

越来越多的APP采用这种样式来控制导航栏的不同风格,今天我们就来实现这一效果。

这里需要使用到一个第三方库

https://github.com/rickytan/RTRootNavigationController

借助这个库我们能够轻松实现这一效果

新建一个工程,这里我们使用cocoapods来集成这个第三方库

集成RTRootNavigationController

podfile

1
2
3
4
5
6
workspace ‘iOS每个VC单独的一个导航栏.xcworkspace’
project ‘iOS每个VC单独的一个导航栏.xcodeproj’
platform :ios, ‘8.0‘
target ‘iOS每个VC单独的一个导航栏‘ do 
pod ‘RTRootNavigationController’
end

使用RTRootNavigationController当做当前的rootController

创建BaseViewController

我这里新建一个BaseViewController 主要是为了引入RTRootNavigationController,当然如果是OC项目的话,可以直接创建一个PCH文件,直接全局引用也行,不过我们一般都会有一个基类的ViewController,在这个基类中,没有做任何操作,只是引用了一个RTRootNavigationController

1
2
3
4
5
#import "RTRootNavigationController.h"
 
@interface BaseViewController : UIViewController
 
@end

设置根控制器

在Appdelegate中,我们需要将我们的window的rootcontroller设置为RTRootNavigationController

1
2
3
4
5
6
7
8
9
10
11
12
13
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 
ViewController *viewController = [[ViewController alloc] init];
 
RTRootNavigationController *rootViewController1 = [[RTRootNavigationController alloc] initWithRootViewController:viewController];
 
_window.rootViewController = rootViewController1;
 
_window.backgroundColor = [UIColor whiteColor];
 
[_window makeKeyAndVisible];
 
return YES;

在ViewController中,我们需要push出去一个vc的时候,我们需要这样实现

1
2
//注意这里push的时候需要使用rt_navigation push出去
[self.rt_navigationController pushViewController:vc1 animated:YES complete:nil];

看一下效果

技术分享

设置返回NavigationBar按钮

在当前的vc中,我们设置返回按钮,或者其他的按钮,也很方便

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 addTarget:self action:@selector(leftBar1Clicked) forControlEvents:UIControlEventTouchUpInside];
[btn1 setTitle:@"返回1" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 sizeToFit];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setTitle:@"返回2" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(leftBar2Clicked) forControlEvents:UIControlEventTouchUpInside];
[btn2 sizeToFit];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:btn2];
self.navigationItem.leftBarButtonItems = @[item1,item2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn3 setTitle:@"右键" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(rightBarClicked) forControlEvents:UIControlEventTouchUpInside];
[btn3 sizeToFit];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn3];
self.navigationItem.rightBarButtonItem = rightItem;
[self.view addSubview:label];

技术分享

多个按钮定义也是很方便的

如果只是需要一个左边的返回按钮,这个按钮需要自定义样式,那么可以直接在当前VC冲下方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 如果对于返回事件不需要做任何处理,
 但是有想要自定义返回按钮的样式,
 可以直接重写这个方法
 @param target 监听对象
 @param action 返回事件
 @return 自定义的返回按钮
 */
-(UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return item;
}

这样的话,就不要去单独设置左上角的返回按钮了

技术分享

跳到最开始的VC

在我们pop的时候,可以直接pop在栈顶的VC

1
[self.rt_navigationController popToRootViewControllerAnimated:YES complete:nil];

push到另外一个VC 销毁当前的VC

有时我们想要实现这样一种效果,当当前的VCpush出去过后,希望销毁当前的VC

1
2
3
4
ViewController4 *vc4 = [[ViewController4 alloc] init];
[self.rt_navigationController pushViewController:vc4 animated:vc4 complete:^(BOOL finished) {
     [self.rt_navigationController removeViewController:self];
 }];

技术分享

更改导航栏颜色

之前忘记更改导航栏的颜色了,这里看一下,更改导航栏的颜色,只需要

 

1
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];

技术分享

总结

如果你的APP在导航栏有多种样式的话,你完全可以使用这种方法,使用起来很方便

感谢:

rickyTan开源

https://github.com/rickytan/RTRootNavigationController

项目的源码我放在了:

https://github.com/yangqian111/blog/tree/master/iOS每个VC单独的一个导航栏

 

iOS每个ViewController一个NavigationBar

标签:item   vcpu   ati   imp   ram   btn   finish   ima   stat   

原文地址:http://www.cnblogs.com/ruixin-jia/p/6087782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!