IOS開發中創建TabBarController和UINavigationController的一種方法

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

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];
    //設置title
    viewController.title = titleArray[i];
    //設置tabbarItem圖片
    UIImage *normalImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_正常",titleArray[i]]];
    UIImage *selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_點擊",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;
//設置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的創建,其中IOS7為:#define IOS7 [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 表示版本的判斷

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

推薦閱讀更多精彩內容

  • 代碼創建UIWindow對象 Xcode7之后使用代碼創建UIWindow對象: //創建UIWindow對象 s...
    云之君兮鵬閱讀 1,370評論 0 2
  • //設置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 838評論 0 0
  • 前言: 以下內容是作者在實際開發中所總結的,主要列舉了一些實用小技巧,也希望在實際開發中能夠幫到你。 設置控件的圓...
    暗香有獨閱讀 1,400評論 6 33
  • 一、多色 1、找到系統配置文件 路徑: 2、添加以下代碼 注:詳細講解代碼中的涵義: 二、自動補全,不區分大小寫 ...
    蠻小刀閱讀 1,412評論 0 1
  • “再回首,背影已遠走;再回首,淚眼朦朧。留下你的祝福,寒夜溫暖我,不管明天要面對多少傷痛和迷惑……” 是啊!再次回...
    宇兒呦呦閱讀 572評論 0 0