@interface CZNavigationController ()<UINavigationControllerDelegate>
@property (nonatomic, strong) id popDelegate;
@end
@implementation CZNavigationController
+ (void)initialize
{
// 获取当前类下面的UIBarButtonItem
UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
// 设置导航条按钮的文字颜色
NSMutableDictionary *titleAttr = [NSMutableDictionary dictionary];
titleAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:titleAttr forState:UIControlStateNormal];
// 注意导航条上按钮不可能,用模型的文字属性设置是不好使
// // 设置不可用
// titleAttr = [NSMutableDictionary dictionary];
// titleAttr[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
// [item setTitleTextAttributes:titleAttr forState:UIControlStateDisabled];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_popDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count) { // 不是根控制器
viewController.hidesBottomBarWhenPushed = YES;
UIBarButtonItem *left = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_back"] highImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] target:self action:@selector(popToPre) forControlEvents:UIControlEventTouchUpInside];
// 设置导航条的按钮
viewController.navigationItem.leftBarButtonItem = left;
UIBarButtonItem *right = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_more"] highImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] target:self action:@selector(popToRoot) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.rightBarButtonItem = right;
}
[super pushViewController:viewController animated:animated];
}
- (void)popToRoot
{
[self popToRootViewControllerAnimated:YES];
}
- (void)popToPre
{
[self popViewControllerAnimated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
NSLog(@"%@",tabBarVc);
// 删除系统自带的tabBarButton
for (UIView *tabBarButton in tabBarVc.tabBar.subviews) {
if (![tabBarButton isKindOfClass:[CZTabBar class]]) {
[tabBarButton removeFromSuperview];
}
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self.viewControllers[0]) { // 是根控制器
self.interactivePopGestureRecognizer.delegate = nil;
}else{ // 非根控制器
self.interactivePopGestureRecognizer.delegate = _popDelegate;
}
}
自定义导航控制器
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- SYNavigationViewController可以自定义不同样式的导航栏样式的导航栏视图控制器(有push动...
- 问题:如果我们在项目中的返回按钮用的是leftBarButtonItem 那么系统自带导航控制器的滑动返回功能失效...
- 在OC开发中,导航控制器是一个非常常见的控件,而且在不少使用时候,我们需要自定义导航条NavigationBar...