新增繼承UIViewController的Controller A和Controller B,並在xib檔裡加入label
在Delegate裡實現
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UITabBarController* myTab = [[UITabBarController alloc] init]; ControllerA* conA = [[ControllerA alloc] initWithNibName:@"ControllerA" bundle:nil]; ControllerB* conB = [[ControllerB alloc] initWithNibName:@"ControllerB" bundle:nil]; conA.tabBarItem.title = @"Controller A"; conA.tabBarItem.image = [UIImage imageNamed:@"apple.png"]; // 這二段也可寫在ControllerA的viewDidLoad方法裡,範例如下 conB.tabBarItem.title = @"Controller B"; myTab.viewControllers = [NSArray arrayWithObjects:conA, conB, nil]; self.window.rootViewController = myTab; return YES; } |
ControllerA.m
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // self.tabBarItem.title = @"Controller A"; // self.tabBarItem.image = [UIImage imageNamed:@"apple.png"]; // 但如此可能會造成一開始不會顯示出title和image的問題,因為viewDidLoad是當view被讀到時才會動作 } |
注意
- 插入image時,需使用.png,否則會造成圖片成方格的問題,因為TabBar的圖在沒有點擊時會讓有顏色的部份變灰白色,透明部份不變。
- UITabBar可見Tab為5個,多的會加入more選項中。
- 若UITabBarController與UINavigationController合用時,以UITabBarController為主,因為在設計上UITabBarController不適合動態新增UIViewController。
資料出處《Object-C與iOS開發入門》
沒有留言:
張貼留言