IOS開發(fā)中創(chuàng)建TabBarController和UINavigationController的一種方法

在IOS開發(fā)中創(chuàng)建TabBarController和UINavigationController是很常見的,如果
是手寫代碼創(chuàng)建而不是用Storyboard創(chuàng)建,由于涉及到較多的ViewController(一
般為五個),顯得比較麻煩,并且假如選擇的方法過于笨拙,則會產(chǎn)生大量的“重復(fù)代
碼”。我在實踐的過程中總結(jié)出下面這樣一種創(chuàng)建方法,來避免大量“重復(fù)代碼”的
出現(xiàn)。
在上代碼之前,首先假設(shè)我要做五個ViewController,他們的類名分別為FirstView
Controller、SecondViewController、ThirdViewcontroller、FourthViewContro
ller、FifthViewController?,F(xiàn)在開始上代碼:

NSArray *classNameArray = @[@"FirstViewController",@"SencondViewController",@"ThirdViewController",@"FourthViewController",@"FifthViewController"];
NSArray *titleArray = @[@"first",@"sencond",@"third",@"fourth",@"fifth"];
NSMutableArray *navigationControllerArray = [[NSMutableArray alloc] init];
for (int i=0; i<classNameArray.count; i++) {
    UIViewController *viewController = [[NSClassFromString(classNameArray[i]) alloc] init];
    //設(shè)置title
    viewController.title = titleArray[i];
    //設(shè)置tabbarItem圖片
    UIImage *normalImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_正常",titleArray[i]]];
    UIImage *selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_點(diǎn)擊",titleArray[i]]];
    if (IOS7) {
        viewController.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        viewController.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    else {
        [viewController.tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:normalImage];
    }
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [array addObject:navigationController];
}
UITabBarController *tabBatController = [[UITabBarController alloc] init];
tabBatController.viewControllers = array;
self.window.rootViewController = tabBatController;
//設(shè)置UITabBarItem屬性
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:14.0f]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:20/255.0 green:152/255.0 blue:172/255.0 alpha:1], NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:14.0f]} forState:UIControlStateSelected];

tabBarController.viewControllers = navigationControllerArray;
tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"item_selected_background.png"];
tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background.png"];

以上代碼即可完成TabBarController和UINavigationController的創(chuàng)建,其中IOS7為:#define IOS7 [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 表示版本的判斷

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

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

  • 代碼創(chuàng)建UIWindow對象 Xcode7之后使用代碼創(chuàng)建UIWindow對象: //創(chuàng)建UIWindow對象 s...
    云之君兮鵬閱讀 1,495評論 0 2
  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 971評論 0 0
  • 前言: 以下內(nèi)容是作者在實際開發(fā)中所總結(jié)的,主要列舉了一些實用小技巧,也希望在實際開發(fā)中能夠幫到你。 設(shè)置控件的圓...
    暗香有獨(dú)閱讀 1,468評論 6 33
  • 一、多色 1、找到系統(tǒng)配置文件 路徑: 2、添加以下代碼 注:詳細(xì)講解代碼中的涵義: 二、自動補(bǔ)全,不區(qū)分大小寫 ...
    蠻小刀閱讀 1,503評論 0 1
  • “再回首,背影已遠(yuǎn)走;再回首,淚眼朦朧。留下你的祝福,寒夜溫暖我,不管明天要面對多少傷痛和迷惑……” 是?。≡俅位?..
    宇兒呦呦閱讀 615評論 0 0

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