標籤:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //初始化 tabBarItem對應的ViewController UIViewController *viewCtrl1 = [[UIViewController alloc] init]; viewCtrl1.title = @"first viewctrl"; viewCtrl1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"rename first" image:nil tag:1];//自己定義tabBarItem是在對應的ViewController中去做 viewCtrl1.view.backgroundColor = [UIColor blueColor]; UIViewController *viewCtrl2 = [[UIViewController alloc] init]; viewCtrl2.title = @"second viewctrl"; viewCtrl2.view.backgroundColor = [UIColor yellowColor]; viewCtrl2.tabBarItem.badgeValue = @"360"; UIViewController *viewCtrl3 = [[UIViewController alloc] init]; viewCtrl3.title = @"3 viewctrl"; viewCtrl3.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.2 blue:0.3 alpha:1.0]; UIViewController *viewCtrl4 = [[UIViewController alloc] init]; viewCtrl4.title = @"4 viewctrl"; viewCtrl4.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.3 alpha:1.0]; UIViewController *viewCtrl5 = [[UIViewController alloc] init]; viewCtrl5.title = @"5 viewctrl"; viewCtrl5.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0]; UIViewController *viewCtrl6 = [[UIViewController alloc] init]; viewCtrl6.title = @"6 viewctrl"; viewCtrl6.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.5 blue:0.7 alpha:1.0]; UIViewController *viewCtrl7 = [[UIViewController alloc] init]; viewCtrl7.title = @"7 viewctrl"; viewCtrl7.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.5 blue:0.3 alpha:1.0]; // tabBarCtrl = [[UITabBarController alloc] init]; tabBarCtrl.viewControllers = [NSArray arrayWithObjects:viewCtrl1, viewCtrl2 ,viewCtrl5, viewCtrl3, viewCtrl4, viewCtrl6, viewCtrl7, nil];//添加viewcontrollers, 數組順序就是tabBarItem對應的viewController順序 tabBarCtrl.customizableViewControllers = [NSArray arrayWithObjects: viewCtrl2 ,viewCtrl3, viewCtrl5, viewCtrl7, nil]; //顯示可編輯的有那些viewcontroller [tabBarCtrl setSelectedIndex: 3];//設定選中的是那個tabBarItem,如果為無效的index,則預設選中第一個tabBarCtrl.view.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height); self.window.rootViewController = tabBarCtrl; [self.window makeKeyAndVisible]; return YES; } 關於旋轉UITabBarController預設只支援豎屏,當裝置方向放生變化時候,它會查詢viewControllers中包含的所有ViewController,僅當所有的viewController都支援該方向時,UITabBarController才會發生旋轉,否則預設的豎向。UITabBarController支援旋轉,而且發生旋轉的時候,只有當前顯示的viewController會接收到旋轉的訊息。 關於 UITabBarControllerDelegate 委託//設定使用者是否可以選中,如果返回NO,使用者不能選中,但代碼中還可以用tabBarCtrl setSelectedIndex: 3]選中- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;{ return YES;}//選中後的訊息響應- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{} more 中edit監測- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
UITabBarController 筆記(一)AppDelegate中加UITabBarController 為 rootViewController