工作很長一段時間了俊犯,搭建框架的時候沒有搭好,然后者祖,需求又一直改變七问,其中導(dǎo)航條和標(biāo)簽欄的修改是比較難受的械巡,特別是完全使用系統(tǒng)的導(dǎo)航條和標(biāo)簽欄讥耗。
比如:
1.修改導(dǎo)航條的返回按鈕的的圖片
2.修改導(dǎo)航返回按鈕的文字顏色和大小
3.去掉導(dǎo)航條的返回文字
4.統(tǒng)一修改導(dǎo)航標(biāo)題的字體樣式和大小
5.修改標(biāo)簽欄的字體樣式和顏色
6.把標(biāo)簽欄的文字去掉只留下圖片
7.把導(dǎo)航條和標(biāo)簽欄的黑線去掉
8 在跳轉(zhuǎn)到下一個頁面的時候隱藏標(biāo)簽欄
這些東西不難,但是要是不熟悉籍琳,產(chǎn)品經(jīng)理要你改的時候趋急,覺得很惡心呜达,下面我就一一來說說這個怎么弄查近,然后我寫一個demo放在github 上提供大家參考霜威,也可以直接使用我寫好的TabbarController框架戈泼。
先看一下效果吧(順便加了大神寫的一個全屏側(cè)滑返回的方法)
先說一下導(dǎo)航條:
新建一個繼承字UINavigationController 的類大猛,在類的.m 文件中
<pre><code> 先設(shè)置導(dǎo)航條不透明
self.navigationBar.translucent = NO;
//設(shè)置導(dǎo)航條的背景顏色
self.navigationBar.barTintColor = Nav_background_Color;
//設(shè)置導(dǎo)航條的主色調(diào)顏色
self.navigationBar.tintColor = Nav_tintColor;</code></pre>
//設(shè)置返回按鈕為自定義圖片
<pre><code>
//設(shè)置返回的箭頭自定義圖片
//imageWithRenderingMode 這個是圖片的顯示模式膛壹,設(shè)置成其他的選項(xiàng)模聋,你的返回按鈕的顏色不是自己圖片顏色
UIImage *backimage =[Nav_back_icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//注意:下面兩個屬性都得設(shè)置撬槽,只設(shè)置一個是無效的
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backimage];
[[UINavigationBar appearance]setBackIndicatorImage:backimage];
</code></pre>
//去掉導(dǎo)航條的返回文字,去掉這個還真的不好去掉,鉆了空子占调,設(shè)置了文字的PositionAdjustmen就可以了
<pre><code>
// 去掉返回按鈕文字
UIBarButtonItem *baritem =[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
UIOffset offset;
offset.horizontal = -500;
[baritem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
</code></pre>
//導(dǎo)航條上文字設(shè)置
<pre><code>
//設(shè)置返回按鈕
[baritem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];<code></code>
//改變導(dǎo)航的title 字體的樣式
<code>NSDictionary *titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:Nav_Title_Color,NSForegroundColorAttributeName,Nav_Title_Font,NSFontAttributeName, nil]; [[UINavigationBar appearance] setTitleTextAttributes:titleAttributes];
</code></pre>
// 這里重寫了系統(tǒng)的push到下一個頁面的方法,使用viewController.hidesBottomBarWhenPushed跳轉(zhuǎn)到下一個頁面的導(dǎo)航條
<pre><code>
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count>0) {
///第二層viewcontroller 隱藏tabbar
viewController.hidesBottomBarWhenPushed=YES;
}
[super pushViewController:viewController animated:YES];
}</code></pre>
導(dǎo)航欄說完了剿涮,下面說下標(biāo)簽欄了取试。
新建一個繼承自UITabBarController的類
<pre><code>
//設(shè)置背景顏色
self.tabBar.barTintColor = Tab_BAR__BACK_CLOLOR;
//設(shè)置標(biāo)簽不透明
self.tabBar.translucent= NO;
//常規(guī)設(shè)置
NSArray *classNameArray =[NSArray arrayWithObjects:@"FirstViewController",@"SecondViewController",@"ThirdViewController", nil];
NSArray *titleArray =[NSArray arrayWithObjects:@"first",@"second",@"third", nil];
NSArray *normalImageArray =[NSArray arrayWithObjects:@"feed_tab_butten_normal.png",@"movie_tab_butten_normal.png",@"me_tab_butten_normal.png",nil];
NSArray *selectImageArray =[NSArray arrayWithObjects:@"feed_tab_butten_press.png",@"movie_tab_butten_press.png",@"me_tab_butten_press.png", nil];
NSMutableArray *navigationArray =[[NSMutableArray alloc]init];
for (int i=0; i<classNameArray.count; i++) {
UIViewController *vc =(UIViewController *)[[NSClassFromString(classNameArray[i]) alloc] init];
vc.title=titleArray[i];
UIImage *normalImage =[UIImage imageNamed:normalImageArray[i]];
UIImage *selectImage =[UIImage imageNamed:selectImageArray[i]];
vc.tabBarItem.image =[normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage=[selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
去掉標(biāo)簽欄的文字蓬坡,也是跟導(dǎo)航欄的文字一樣的道理屑咳,設(shè)置便宜兆龙,巧妙的去掉了標(biāo)簽欄文字,這里設(shè)置后臣缀,下面設(shè)置字體樣式就沒有意義了精置。
UIOffset offoset = UIOffsetMake(0, 300);
[vc.tabBarItem setTitlePositionAdjustment:offoset];
BaseNavigationViewController *na =[[BaseNavigationViewController alloc]initWithRootViewController:vc];
[navigationArray addObject:na];
}
self.viewControllers=navigationArray;
//標(biāo)簽欄的文字設(shè)置
UITabBarItem *item = [UITabBarItem appearance];
//正常狀態(tài)的文字
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:Tab_TITLE_NORMAL_COLOR, NSFontAttributeName:Tab_TITLE_FONT} forState:UIControlStateNormal];
//選擇的文字
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:Tab_TITLE_SELECTED_COLOR, NSFontAttributeName:Tab_TITLE_FONT} forState:UIControlStateSelected];</code></pre>
//差點(diǎn)忘了說怎么修改導(dǎo)航條和標(biāo)簽欄下面黑線的顏色了,這個imageWithColor:是我寫的把顏色轉(zhuǎn)化成圖片的類脂倦。
<pre>[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:VLight_GrayColor_apla]];
[self.tabBarController.tabBar setShadowImage:[UIImage imageWithColor:VLight_GrayColor_apla]];
</pre>
好了,到這里火欧,高度自定義的導(dǎo)航條和標(biāo)簽欄就完成了苇侵,而且我們完全使用系統(tǒng)的企锌,由于本人技術(shù)有限陡鹃,所有希望大家有什么問題斧正啊抖坪。
代碼我放在了github上 地址是
ZFYTabbarController