前言
博主已經(jīng)做iOS有一段時間了笼踩,最近沒有項目也不想閑著莫矗,總結(jié)一下自己學(xué)到的一些實戰(zhàn)項目中遇到的問題以及經(jīng)驗咽弦,分享給剛?cè)腴T的初學(xué)者,希望能幫助他們少走彎路胎挎。
本文主要談?wù)勛远xtabBar如何實現(xiàn)沟启,導(dǎo)航控制器是如何設(shè)置和工作的,以及在使用中遇到的一些問題和解決方案呀癣。
正文
1美浦、如何自定義一個tabBar,創(chuàng)建一個UITabBarController子類项栏,封裝一些底層處理邏輯浦辨。
.h代碼,定義一些需要自定義的屬性(關(guān)鍵代碼):
@interface ZciotBaseTabbarController : UITabBarController
@property (nonatomic,strong)NSMutableArray *navs;
@property (nonatomic,strong)NSArray *views;
@property (nonatomic,strong)NSArray *tabTitles;
@property (nonatomic,strong)NSArray *tabNorImgs;
@property (nonatomic,strong)NSArray *tabSelImgs;
@property (nonatomic,strong)UIColor *titleHighlightedColor;
@property (nonatomic,strong)UIColor *titleNorColor;
@end
.m代碼沼沈,for循環(huán)設(shè)置控制器以及控制器的一些屬性:
// rgb顏色轉(zhuǎn)換(16進制->10進制)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
- (void)viewDidLoad {
[super viewDidLoad];
_navs = [NSMutableArray new];
for (int i = 0 ; i<_views.count; i++) {
ZciotNavigationController *nav = [[ZciotNavigationController alloc]initWithRootViewController:_views[i]];//這里是樓主自定義的導(dǎo)航控制器流酬,可以用系統(tǒng)的代替。
UIImage *norImg = [UIImage imageNamed:_tabNorImgs[i]];
UIImage *selImg = [UIImage imageNamed:_tabSelImgs[i]];
[norImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav.tabBarItem.title = _tabTitles[i];
nav.tabBarItem.selectedImage = selImg;
nav.tabBarItem.image = norImg;
nav.topViewController.title = _tabTitles[i];//導(dǎo)航欄標(biāo)題
[_navs addObject:nav];
}
_titleHighlightedColor = UIColorFromRGB(0xea6d66);
_titleNorColor = UIColorFromRGB(0x868686);
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:_titleNorColor} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:_titleHighlightedColor} forState:UIControlStateSelected];
self.viewControllers = _navs;
self.tabBarController.selectedViewController = _navs[0];//默認(rèn)第一個控制器選中
self.tabBar.translucent = NO;
// Do any additional setup after loading the view.
}
2列另、創(chuàng)建一個XXXTabBarController繼承上面的基類芽腾,給父類屬性賦值(關(guān)鍵代碼):
@interface ZciotTabbarController ()
{
NSArray *vcs;
}
- (void)viewDidLoad {
ViewController *vc = [ViewController new];
ViewController1 *vc1 = [ViewController1 new];
vcs = @[vc,vc1];
super.views = vcs;
super.tabTitles = @[@"控制器1",@"控制器2"];
super.tabNorImgs = @[@"footbar_icon_off_03",@"footbar_icon_off_03"];
super.tabSelImgs = @[@"footbar_icon_off_03",@"footbar_icon_off_03"];
[super viewDidLoad];//注意該句代碼一定要放到賦值之后,否則會報錯页衙,因為父類沒取到值摊滔。
self.delegate = (id)self;
// Do any additional setup after loading the view.
}
最后在AppDelegate.m里設(shè)置根控制器就行了:
ZciotTabbarController *vc =[ZciotTabbarController new];
self.window.rootViewController = vc;
注意,這時候你會發(fā)現(xiàn)一個問題店乐,就是:
想跳轉(zhuǎn)隱藏tabBar用系統(tǒng)的self.navigationController.hidesBottomBarWhenPushed=YES;
根本就不管用艰躺,原因在于我的tabbar是我自定義的,所以這個命令自然就不生效了眨八,對此我也在網(wǎng)上查了好多辦法腺兴,有的說push的時候?qū)abbar移除屏幕外 回來的時候在放回來,等等页响,試驗了一下也沒試出來篓足,后來我就想,干脆在appdelegate里面不用自定義的tabbar當(dāng)根了闰蚕,用navigationcontroller當(dāng)根诗鸭,然后把自定義tabbar類里面的viewcontroller 的導(dǎo)航都去掉妓盲,不就OK 了,這樣當(dāng)我想push的時候专普,什么煩惱都沒有了悯衬,直接push。
這個問題引用來源:http://blog.csdn.net/yudandan10/article/details/42341501
3檀夹、關(guān)于導(dǎo)航控制器
這里推薦一篇優(yōu)秀博客筋粗,里面介紹得很詳細(xì):
http://blog.cocoachina.com/article/23772
博主就補充幾點:
(1)如果你tabBar控制的視圖控制器導(dǎo)航都一樣,那么就在設(shè)置根視圖控制器的時候設(shè)置導(dǎo)航的屬性炸渡,如果你是每個視圖控制器的導(dǎo)航都不一樣娜亿,那么就在賦值控制器給tabBar的時候設(shè)置導(dǎo)航欄屬性。
(2)如果你想自定義導(dǎo)航欄按鈕和視圖蚌堵,就在當(dāng)前視圖控制器進行設(shè)置买决。(通常設(shè)置代碼寫在viewWillapper里)
(3)博主對tabBar以及導(dǎo)航欄的理解:
tabBar其實就是一個存放viewController的容器,而導(dǎo)航控制器他擁有系統(tǒng)的tabBar屬性吼畏,每個viewController都自帶有導(dǎo)航欄屬性督赤。其實就像一個可以裝多個小孩的背簍,tabbar就充當(dāng)背簍的角色宫仗,而小孩就是當(dāng)中的viewController够挂,而導(dǎo)航欄就相當(dāng)于每個孩子頭上戴的帽子。當(dāng)你理解他們的關(guān)系的時候藕夫,用自定義的tabBar便會柔韌有余孽糖。
總結(jié)
其實這些理解起來并不難枯冈,但是博主也是接觸了很多項目,試驗了很多方式才大概理解办悟,這篇文章希望對剛?cè)腴T的初學(xué)者有所幫助尘奏,幫助你們少走彎路。
博主的微博病蛉、CocoaChina博客炫加、CSDN博客同步更新,歡迎關(guān)注:
新浪微博:http://weibo.com/p/1005052308506177/home?from=page_100505_profile&wvr=6&mod=data&is_all=1#place
CocoaChina:http://blog.cocoachina.com/477998
CSDN:http://blog.csdn.net/czkyes