iOS开发-自定义导航栏UINavigationController

负责整个项目的UINavigationController

不管是主流的tabbarController+NavigationController,还是独立出来的NavigationController,整个项目中使用自定义的UINavigationController,方便管理修改。

下面的自定义的UINavigationController,解决了手势返回的问题。

CJNavigationController.h

//
//  CJNavigationController.h
//  Personal Application
//
//  Created by  zengchunjun on 16/3/14.
//  Copyright © 2016年  zengchunjun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CJNavigationController : UINavigationController

//设置一个属性保存系统的代理
@property (nonatomic, strong) id popDelegate;

@end

CJNavigationController.m
复制过去后报错的地方自行修改。

//
//  CJNavigationController.m
//  Personal Application
//
//  Created by  zengchunjun on 16/3/14.
//  Copyright © 2016年  zengchunjun. All rights reserved.
//

#import "CJNavigationController.h"
#import "DiffUtil.h"

@interface CJNavigationController ()<UINavigationControllerDelegate>

@end

@implementation CJNavigationController

// 第一次用到这个类时使用这个方法,只执行一次
+ (void)initialize{
    
    [super initialize];
    
    // 设置UIBarButtonItem的属性
    // 获取当前类下得item
    UIBarButtonItem *items = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    
    NSMutableDictionary *att = [NSMutableDictionary dictionary];
    att[NSForegroundColorAttributeName] = [UIColor whiteColor];
    // item字体的大小
    att[NSFontAttributeName] = [DiffUtil getDifferFont:17];
    [items setTitleTextAttributes:att forState:UIControlStateNormal];
    
    
    
    //设置UINavigationBar的属性
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    bar.translucent = NO;
//    UIImage *barImage = [UIImage imageWithColor:[DiffUtil getDifferColor] andSize:bar.bounds.size];
//    [bar setBackgroundImage:barImage forBarMetrics:UIBarMetricsDefault];
//    [bar setBarTintColor:[UIColor whiteColor]];//
    [bar setBarTintColor:[DiffUtil getDifferColor]];
    
    [bar setTintColor:[UIColor whiteColor]];
    
    NSMutableDictionary *barAtt = [NSMutableDictionary dictionary];
    // 设置标题的字体
    barAtt[NSFontAttributeName] = [DiffUtil getDifferFont:17];
    // 设置标题的颜色(前景色)
    barAtt[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    //设置item的颜色
    [bar setTitleTextAttributes:barAtt];
    
    // 设置状态栏格式
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;


}

// 推出下一个界面时都会调用这个方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.interactivePopGestureRecognizer.delegate = nil;
    
    if (self.childViewControllers.count) {// 非根控制器
        // 隐藏底部的tabbar
//         viewController.hidesBottomBarWhenPushed = YES;
        
        // 设置非控制器,左侧返回右侧返回根控制器按钮图片
        UIImage *image = [[UIImage imageNamed:@"icon_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backPre)];
        viewController.navigationItem.leftBarButtonItem = back;
    }
    
    // 调用父类的方法才会实现推出下一个界面的效果
     [super pushViewController:viewController animated:YES];
   
}

// 返回上一个界面
- (void)backPre
{
    [self popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //判断控制器是否为根控制器
    if (self.childViewControllers.count) {
        //将保存的代理赋值回去,让系统保持原来的侧滑功能
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //将系统的代理保存(在view加载完毕就赋值--->viewDidLoad)
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,743评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,723评论 7 249
  • “雾锋黯无色,霜旗冻不翻。耿介倚长剑,日落风尘昏”。 在过去的一天中,我总算见识到了什么叫做持久战。 亚特兰大老鹰...
    木中心阅读 4,429评论 4 73
  • 佛教的影子在我们生活中其实到处都是,各种电视剧里面,还有我们日常习俗要去庙里烧香等等。都容易让我们对佛教产生好感。...
    部落10阅读 4,166评论 0 0
  • 2016.4.20金币7 收获的季节到了,辛苦的劳动终于看见成果了。
    304860777ae6阅读 3,089评论 0 0

友情链接更多精彩内容