RTRootNavigationController是iOS開發里面經常用到的第三方框架,可以用cocoapods導入項目中,使用TRootNavigationController可以自定義導航欄
參考:http://www.cocoachina.com/ios/20161121/18137.html
-
TRootNavigationController的使用
TRootNavigationController可以達到這個效果:頁面手勢返回時 導航條跟隨控制器一起滑動
B8CA0FAFD597D39B281725D10D42F92E.jpg
1.在AppDelegate里面設置根視圖
AppDelegate.m
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setBackgroundColor:[UIColor whiteColor]];
self.window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewController:[[CFFRootViewController alloc] init]];
[self.window makeKeyAndVisible];
2.在base類導入一下TRootNavigationController.h頭文件就行,也可以把TRootNavigationController.h放到pch文件里面,base類,每個vc都需要繼承它
base.h
#import <UIKit/UIKit.h>
//導入一下
#import <RTRootNavigationController/RTRootNavigationController.h>
@interface CFFBaseViewController : UIViewController
@end
base.m
//重寫RTRootNavigationController里方法,修改返回按鈕;在其他控制器想設置導航欄不一樣,可以在其他控制器重寫該方法修改當前控制器的導航欄
- (UIBarButtonItem *)rt_customBackItemWithTarget:(id)target action:(SEL)action {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
[btn sizeToFit];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:btn];
}
3.如果有底部欄,在root根視圖里面
//RTContainerNavigationController是RTRootNavigationController導航欄的類
UIViewController *vc1 = [CFFMainViewController new];
RTContainerNavigationController *nc1 = [[RTContainerNavigationController alloc] initWithRootViewController:vc1];
vc1.tabBarItem.title = @"首頁";
UIViewController *vc2 = [CFFDynamicViewController new];
RTContainerNavigationController *nc2 = [[RTContainerNavigationController alloc] initWithRootViewController:vc2];
vc2.tabBarItem.title = @"動態";
UIViewController *vc3 = [CFFMeViewController new];
RTContainerNavigationController *nc3 = [[RTContainerNavigationController alloc] initWithRootViewController:vc3];
nc2.rdv_tabBarItem.title = @"我的";
UIViewController *centerVC = [[ViewController alloc] init];
RTContainerNavigationController *ncCenter = [[RTContainerNavigationController alloc] initWithRootViewController:centerVC];
self.viewControllers = @[nc1,nc2,ncCenter,nc3];
4.視圖跳轉
CFFPlayViewController *playVC = [[CFFPlayViewController alloc] init];
//導入RTRootNavigationController,依然可以用自帶的導航欄跳轉
[self.navigationController pushViewController:playVC animated:YES];
//使用RTRootNavgationController的跳轉方式
// [self.rt_navigationController pushViewController:playVC animated:YES complete:nil];
- 導航欄屬性修改
//修改導航欄顏色 可以設置barTintColor
tintColor:item字體顏色(修改所有界面的導航欄按鈕,如果使用了RTRootNavigatonController,則設置該屬性,只改變當前導航欄按鈕) barTintColor:導航欄背景顏色(修改所有界面的導航欄,如果使用了RTRootNavigatonController,則設置該屬性,只改變當前導航欄)
self.navigationController.navigationBar.barTintColor = [UIColor groupTableViewBackgroundColor];
修改導航欄顏色 也可以導航欄背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor grayColor]] forBarMetrics:UIBarMetricsDefault]; //導航欄背景圖片
//隱藏導航欄分割線
[self.navigationController.navigationBar setClipsToBounds:YES];
//修改導航欄分割線 設置陰影分割線,和背景圖片一起設置才有效果
如果想隱藏分割線,也可以陰影分割線,和背景圖片設置一樣的顏色
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor redColor]]]; //導航欄分割線
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor grayColor]] forBarMetrics:UIBarMetricsDefault]; //導航欄背景圖片