iOS开发之-Tab Bar Controllers

本文详细介绍了iOS中TabBarController的使用方法,包括如何创建TabBar界面、自定义TabBarController、通过代码和故事板添加TabBarItem等内容,并提供了运行时管理Tab的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tab Bar Controllers


The Tab Bar Interface





The Objects of a Tab Bar Interface





1. If you add more than five items to the viewControllers property, the tab bar controller automatically inserts a special view controller (called the More view controller) to handle the display of the additional items. You can get a reference to it though by accessing the moreNavigationController property of UITabBarController.



2. You must configure the tab bar item of each root view controller prior to displaying the tab bar interface. 


Creating a Tab Bar Interface


1. Of course, it is still possible to present a tab bar controller modally if a very specific need makes doing so worthwhile. 


Defining the Custom View Controllers for a Tab Bar Interface





Creating a Tab Bar Interface Using a Nib File

 



1. A tab bar controller always creates its view programmatically and so there is no view to put in a separate nib file. One consequence of this behavior is that a tab bar controller never manages a nib file—in other words, you never assign a tab bar controller to the File’s Owner placeholder. Instead, the only time you mix tab bar controllers and nib files is when the tab bar controller itself is stored in the nib file.


Creating a Tab Bar Interface Programmatically


- (void)applicationDidFinishLaunching:(UIApplication *)application {
   tabBarController = [[UITabBarController alloc] init];
 
   MyViewController* vc1 = [[MyViewController alloc] init];
   MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
 
   NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
   tabBarController.viewControllers = controllers;
 
   // Add the tab bar controller's current view as a subview of the window
   [window addSubview:tabBarController.view];
}


Creating a Tab Bar Item Programmatically


- (id)init {
   if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
      self.title = @"My View Controller";
 
      UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
      UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
      self.tabBarItem = theItem;
      [theItem release];
   }
   return self;
}

1. You do this by assigning the tab bar item to the tabBarItem property of the corresponding view controller. The ideal time to create a tab bar item is during the initialization of the view controller itself but this is typically feasible only for custom view controllers. 


Managing Tabs at Runtime


Adding and Removing Tabs

- (IBAction)processUserInformation:(id)sender
{
   // Call some app-specific method to validate the user data.
   // If the custom method returns YES, remove the tab.
   if ([self userDataIsValid])
   {
      NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
      [newArray removeObject:self];
 
      [self.tabBarController setViewControllers:newArray animated:YES];
   }
}

Preventing the Selection of Tabs


- (BOOL)tabBarController:(UITabBarController *)aTabBar
         shouldSelectViewController:(UIViewController *)viewController
{
   if (![self hasValidLogin] && (viewController != [aTabBar.viewControllers objectAtIndex:0]) )
   {
      // Disable all but the first tab.
      return NO;
   }
 
   return YES;
}

If you need to prevent the user from selecting a tab, you can do so by providing a delegate object and implementing the tabBarController:shouldSelectViewController: method on that object. Preventing the selection of tabs should be done only on a temporary basis


Monitoring User-Initiated Tab Changes

There are two types of user-initiated changes that can occur on a tab bar:

  • The user can select a tab.
  • The user can rearrange the tabs.

Visual changes of that nature are best handled by your custom view controllers.



Preventing the Customization of Tabs



1. In these situations, you can assign an array of view controller objects to the customizableViewControllers property. This array should contain the subset of view controllers that it is all right to rearrange. View controllers not in the array are not displayed on the configuration screen and cannot be removed from the tab bar if they are already there.

2.  Adding or removing view controllers in your tab bar interface also resets the array of customizable view controllers to the default value, allowing all view controllers to be customized again. Therefore, if you make modifications to the viewControllers property (either directly or by calling the setViewControllers:animated: method) and still want to limit the customizable view controllers, you must also update the array of objects in the customizableViewControllers property.


Changing a Tab’s Badge


if (self.numberOfNewItems == 0)
   self.tabBarItem.badgeValue = nil;
else
   self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", self.numberOfNewItems];

To assign a badge to a tab, assign a non-nil value to the badgeValue property of the corresponding tab bar item.

However, if your view controller contains a property with such a value, you can use KVO notifications to detect changes to the value and update the badge accordingly. 


Tab Bar Controllers and View Rotation

Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the root view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.


Tab Bars and Full-Screen Layout

The tab bar controller always resizes your view to prevent it from underlapping the tab bar.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值