轉自 http://blog.csdn.net/ssrrxx111/article/details/48315843
1、iOS中最常見的一種布局,在底部設定Tab導航,分別控制不同的視圖。
2、使用代碼的方式建立UITabBarController。
1)在AppDelegate的
[html] view plain copy print ? - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,建立UITabBarController,然後建立自己需要的UIViewController或者UITableViewController。將這些視圖作為UINavigationController的根視圖。使用方法 initWithRootViewController。
2)設定UITabBarController的viewControllers屬性,這裡是一個數組,可以把之前的UINavigationController設定在數組中。
3)擷取UITabBarController的tabBar屬性。然後建立多個UITabBarItem與上面的視圖對應起來。設定UITabBarItem的title和image,分別對應標題和映像。
在tabbarcontroller中最多隻能放4個tab,如果超過了4個,那麼後面的都會自動產生一個More的tab,然後將多餘的tab項放在More對應的TableViewController視圖中。
使用代碼建立UITabBarController的代碼如下:
[objc] view plain copy print ? - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. MainController *mainController = [[MainController alloc] init]; BaseNavigationController *mainNavCtl = [self initlizerNavigationControllerWithRootViewController:mainController]; MainController *mainController2 = [[MainController alloc] init]; BaseNavigationController *mainNavCtl2 = [self initlizerNavigationControllerWithRootViewController:mainController2]; MainController *mainController3 = [[MainController alloc] init]; BaseNavigationController *mainNavCtl3 = [self initlizerNavigationControllerWithRootViewController:mainController3]; MainController *mainController4 = [[MainController alloc] init]; BaseNavigationController *mainNavCtl4 = [self initlizerNavigationControllerWithRootViewController:mainController4]; //設定底部tabbar UITabBarController *tab = [[UITabBarController alloc] init]; tab.viewControllers = @[mainNavCtl,mainNavCtl2,mainNavCtl3,mainNavCtl4]; UITabBar *tabBar = tab.tabBar; UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2]; UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];