【IOS】自定義UINavigationController看這篇就夠了

這篇文章足以應對90%的APP應用定制化的需求了.

首先我們創建一個類JDCustomNavigationController繼承UINavigationController

需要創建UINavigationController的時候只要用JDCustomNavigationController代替即可

默認導航欄應該是這個樣子的:


1.修改背景顏色:
在.m中實現如下方法

+ (void)initialize {
    //appearance方法返回一個導航欄的外觀對象
    //修改了這個外觀對象,相當于修改了整個項目中的外觀
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    //設置導航欄背景顏色
    [navigationBar setBarTintColor:JDRGBColor(55,207,240,1)];
}

導航欄將會變成這樣:


2.設置導航欄標題顏色

   //設置標題欄顏色
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName : [UIFont systemFontOfSize:18]};
    

3.設置導航欄按鈕顏色

    //設置NavigationBarItem文字的顏色
  [navigationBar setTintColor:[UIColor whiteColor]];

導航欄就變成這個樣子了:


但是我們push其他頁面的時候導航欄是這樣個樣子的


怎么修改返回按鈕的文字呢?

-只需要重寫下邊的方法:

//重寫push后返回按鈕的文字,文字可以為空字符串.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //修改返回文字
    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
  
    [super pushViewController:viewController animated:animated];
}

就會變成這樣子了:


那如果我們連文字都不想要,想要完全的一個返回箭頭或者其他圖片怎么辦呢?
1.你可以將上邊的返回文字改為@"",這樣雖然可以,但是返回箭頭偏左,不太美觀,如下圖:



那怎么辦呢?
干脆重寫返回按鈕
但是要注意我的寫法:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //全部修改返回按鈕,但是會失去右滑返回的手勢
    if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >=1) {
        
        viewController.navigationItem.leftBarButtonItem = [self creatBackButton];
    }
    
    [super pushViewController:viewController animated:animated];
}


-(UIBarButtonItem *)creatBackButton
{
    return [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];
    
}
-(void)popSelf
{
    [self popViewControllerAnimated:YES];
}

用的是leftBarButtonItem而不是backBarButtonItem哦,具體區別請看這里
leftBarButtonItem與backBarButtonItem的區別

if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >=1) 

這個判斷是為了最底層的viewcontroller不至于加上我們自定義的按鈕
運行之后的效果:

但是有個我們,我發現系統的右滑返回失效了,怎么辦呢?
別著急,只要在viewDidload中添加以下方法即可

//重寫了leftbarItem之后,需要添加如下方法才能重新啟用右滑返回
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = nil;
    }

這樣足夠使用了吧.

另外附隱藏某個導航欄背景透明的方法

 - (void)viewWillAppear:(BOOL)animated
 {
   [super viewWillAppear:animated];
   //設置導航欄背景圖片為一個空的image,這樣就透明了
   [self.navigationBar setBackgroundImage:[[UIImage alloc] init]     forBarMetrics:UIBarMetricsDefault];
   //去掉透明后導航欄下邊的黑邊
   [self.navigationBar setShadowImage:[[UIImage alloc] init]];
 }
 
 - (void)viewWillDisappear:(BOOL)animated{
 
   //如果不想讓其他頁面的導航欄變為透明 需要重置
   [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
   [self.navigationController.navigationBar setShadowImage:nil];
 }

其他小知識點:

    //設置導航欄文字的主題
     NSShadow *shadow = [[NSShadow alloc]init];
     [shadow setShadowOffset:CGSizeZero];
     [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSShadowAttributeName : shadow}];
     [navigationBar setBackgroundImage:[UIImage imageNamed:@"ic_cell_bg_selected"] forBarMetrics:UIBarMetricsDefault];
     //修改所有UIBarButtonItem的外觀
     UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
     
     // 修改item的背景圖片
     //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
     //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background_pushed.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
     //修改item上面的文字樣式
     NSDictionary *dict =@{NSForegroundColorAttributeName : [UIColor whiteColor],NSShadowAttributeName : shadow};
     [barButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
     [barButtonItem setTitleTextAttributes:dict forState:UIControlStateHighlighted];
     //修改返回按鈕樣式
     [barButtonItem setBackButtonBackgroundImage:[UIImage imageNamed:NAVIGATION_BAR_BACK_ICON_NAME] forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
     //設置狀態欄樣式
     [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];

Demo地址:https://github.com/yuying2012/WJDStudyLibrary
這是一個大工程,請從工程中尋找相關模塊代碼.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容