這篇文章足以應(yīng)對90%的APP應(yīng)用定制化的需求了.
首先我們創(chuàng)建一個(gè)類JDCustomNavigationController
繼承UINavigationController
需要?jiǎng)?chuàng)建UINavigationController的時(shí)候只要用JDCustomNavigationController代替即可
默認(rèn)導(dǎo)航欄應(yīng)該是這個(gè)樣子的:
1.修改背景顏色:
在.m中實(shí)現(xiàn)如下方法
+ (void)initialize {
//appearance方法返回一個(gè)導(dǎo)航欄的外觀對象
//修改了這個(gè)外觀對象珍德,相當(dāng)于修改了整個(gè)項(xiàng)目中的外觀
UINavigationBar *navigationBar = [UINavigationBar appearance];
//設(shè)置導(dǎo)航欄背景顏色
[navigationBar setBarTintColor:JDRGBColor(55,207,240,1)];
}
導(dǎo)航欄將會變成這樣:
2.設(shè)置導(dǎo)航欄標(biāo)題顏色
//設(shè)置標(biāo)題欄顏色
navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName : [UIFont systemFontOfSize:18]};
3.設(shè)置導(dǎo)航欄按鈕顏色
//設(shè)置NavigationBarItem文字的顏色
[navigationBar setTintColor:[UIColor whiteColor]];
導(dǎo)航欄就變成這個(gè)樣子了:
但是我們push其他頁面的時(shí)候?qū)Ш綑谑沁@樣個(gè)樣子的
怎么修改返回按鈕的文字呢?
-只需要重寫下邊的方法:
//重寫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];
}
就會變成這樣子了:
那如果我們連文字都不想要,想要完全的一個(gè)返回箭頭或者其他圖片怎么辦呢?
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哦,具體區(qū)別請看這里
leftBarButtonItem與backBarButtonItem的區(qū)別
if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >=1)
這個(gè)判斷是為了最底層的viewcontroller不至于加上我們自定義的按鈕
運(yùn)行之后的效果:
但是有個(gè)我們,我發(fā)現(xiàn)系統(tǒng)的右滑返回失效了,怎么辦呢?
別著急,只要在viewDidload中添加以下方法即可
//重寫了leftbarItem之后,需要添加如下方法才能重新啟用右滑返回
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = nil;
}
這樣足夠使用了吧.
另外附隱藏某個(gè)導(dǎo)航欄背景透明的方法
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//設(shè)置導(dǎo)航欄背景圖片為一個(gè)空的image爆存,這樣就透明了
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//去掉透明后導(dǎo)航欄下邊的黑邊
[self.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)viewWillDisappear:(BOOL)animated{
//如果不想讓其他頁面的導(dǎo)航欄變?yōu)橥该?需要重置
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
}
其他小知識點(diǎn):
//設(shè)置導(dǎo)航欄文字的主題
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];
//設(shè)置狀態(tài)欄樣式
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
Demo地址:https://github.com/yuying2012/WJDStudyLibrary
這是一個(gè)大工程,請從工程中尋找相關(guān)模塊代碼.