歡迎你們關注個人公衆號,我會按期分享一些我在項目中遇到問題的解決辦法和一些iOS實用的技巧,現階段主要是整理出一些基礎的知識記錄下來
ios
文章也會同步更新到個人博客:
ppsheep.comgit
在平常開發中,咱們常常會碰到一些關於導航欄的問題,例如視覺設計,常常性的改變NavigationBar的風格,雖然咱們可以在viewwillApper中來進行處理,可是老是太麻煩,並且須要寫不少多餘的代碼,今天就來說講這種效果,其實已經有不少APP都是使用這種效果github
咱們先來看看已經有的一些APP使用的這種效果xcode
這是天貓APP的效果,注意觀察他的導航欄ide
這是網易新聞,注意看導航欄spa
愈來愈多的APP採用這種樣式來控制導航欄的不一樣風格,今天咱們就來實現這一效果。設計
這裏須要使用到一個第三方庫3d
藉助這個庫咱們可以輕鬆實現這一效果orm
新建一個工程,這裏咱們使用cocoapods來集成這個第三方庫
podfile
workspace ‘iOS每一個VC單獨的一個導航欄.xcworkspace’
project ‘iOS每一個VC單獨的一個導航欄.xcodeproj’
platform :ios, '8.0'
target 'iOS每一個VC單獨的一個導航欄' do
pod ‘RTRootNavigationController’
end複製代碼
我這裏新建一個BaseViewController 主要是爲了引入RTRootNavigationController,固然若是是OC項目的話,能夠直接建立一個PCH文件,直接全局引用也行,不過咱們通常都會有一個基類的ViewController,在這個基類中,沒有作任何操做,只是引用了一個RTRootNavigationController
#import "RTRootNavigationController.h"
@interface BaseViewController : UIViewController
@end複製代碼
在Appdelegate中,咱們須要將咱們的window的rootcontroller設置爲RTRootNavigationController
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的時候,咱們須要這樣實現
//注意這裏push的時候須要使用rt_navigation push出去
[self.rt_navigationController pushViewController:vc1 animated:YES complete:nil];複製代碼
看一下效果
在當前的vc中,咱們設置返回按鈕,或者其餘的按鈕,也很方便
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衝下方法
/**
若是對於返回事件不須要作任何處理,
可是有想要自定義返回按鈕的樣式,
能夠直接重寫這個方法
@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;
}複製代碼
這樣的話,就不要去單獨設置左上角的返回按鈕了
在咱們pop的時候,能夠直接pop在棧頂的VC
[self.rt_navigationController popToRootViewControllerAnimated:YES complete:nil];複製代碼
有時咱們想要實現這樣一種效果,噹噹前的VCpush出去事後,但願銷燬當前的VC
ViewController4 *vc4 = [[ViewController4 alloc] init];
[self.rt_navigationController pushViewController:vc4 animated:vc4 complete:^(BOOL finished) {
[self.rt_navigationController removeViewController:self];
}];複製代碼
以前忘記更改導航欄的顏色了,這裏看一下,更改導航欄的顏色,只須要
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];複製代碼
總結
若是你的APP在導航欄有多種樣式的話,你徹底可使用這種方法,使用起來很方便
感謝:
rickyTan開源
github.com/rickytan/RT…
項目的源碼我放在了:
github.com/yangqian111…