iOS開發(fā)-自定義導航欄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

//設置一個屬性保存系統(tǒng)的代理
@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

// 第一次用到這個類時使用這個方法,只執(zhí)行一次
+ (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];
    
    // 設置狀態(tài)欄格式
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;


}

// 推出下一個界面時都會調(diào)用這個方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.interactivePopGestureRecognizer.delegate = nil;
    
    if (self.childViewControllers.count) {// 非根控制器
        // 隱藏底部的tabbar
//         viewController.hidesBottomBarWhenPushed = YES;
        
        // 設置非控制器,左側(cè)返回右側(cè)返回根控制器按鈕圖片
        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;
    }
    
    // 調(diào)用父類的方法才會實現(xiàn)推出下一個界面的效果
     [super pushViewController:viewController animated:YES];
   
}

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

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //判斷控制器是否為根控制器
    if (self.childViewControllers.count) {
        //將保存的代理賦值回去,讓系統(tǒng)保持原來的側(cè)滑功能
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //將系統(tǒng)的代理保存(在view加載完畢就賦值--->viewDidLoad)
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
}

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


@end

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,320評論 4 61
  • Swift版本點擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,107評論 7 249
  • “霧鋒黯無色,霜旗凍不翻。耿介倚長劍,日落風塵昏”。 在過去的一天中,我總算見識到了什么叫做持久戰(zhàn)。 亞特蘭大老鷹...
    木中心閱讀 1,002評論 4 73
  • 佛教的影子在我們生活中其實到處都是,各種電視劇里面,還有我們?nèi)粘A曀滓R里燒香等等。都容易讓我們對佛教產(chǎn)生好感。...
    部落10閱讀 841評論 0 0
  • 2016.4.20金幣7 收獲的季節(jié)到了,辛苦的勞動終于看見成果了。
    304860777ae6閱讀 446評論 0 0

友情鏈接更多精彩內(nèi)容