The UITabBarController
class allows users to move between multiple view controllers and to
customize the bar at the bottom of the screen. This is best seen in the
YouTube and iPod applications. Both offer one-tap access to different
views, and both offer a More button leading to user selection and
editing of the bottom bar.
With tab bars, you don’t push views the way you do
with navigation bars. Instead, you assemble a collection of controllers
(they can individually be UIViewControllers, UINavigationControllers, or any other kind of view controllers) and add them into a tab bar by setting the bar’s viewControllers property. It really is that simple. Cocoa Touch does all the rest of the work for you. Set allowsCustomizing to YES to enable user reordering of the bar.
Recipe 1 creates 11 simple view controllers of the BrightnessController class. This class uses a UIView embedded into mainview.xib and sets its background to a specified gray level, in this case from 0% to 100% in steps of 10%. Figure 1 (left) shows the interface in its default mode, with the first four items and a More button displayed.
Reorder these tabs by selecting the More option and then tapping Edit. This opens the Configure panel shown in Figure 1 (right). These 11 view controllers are the options a user can navigate through and select from.
Notice that this recipe adds those 11 controllers
twice. The first time assigns them to the list of view controllers
available to the user:
tbarController.viewControllers = controllers;
The second time specifies that the user can select from the entire list when interactively customizing the bottom tab bar:
tbarController.customizableViewControllers = controllers;
The second line is optional, the first mandatory.
After setting up the view controllers, you can add all or some to the
customizable list. If you don’t, you still can see the extra view
controllers using the More button, but users won’t be able to include
them in the main tab bar on demand.
Tab art appears inverted in color on the More
screen. According to Apple, this is the expected and proper behavior.
They have no plans to change this. It does provide an interesting view
contrast when your 100% white swatch appears as pure black on that
screen.
.
Recipe 1. Creating a Tab View Controller
@implementation BrightnessController
- (UIImage*) buildSwatch: (float) tint
{
CGContextRef context = [GraphicsUtilities
newBitmapContextWithWidth:30 andHeight:30];
[GraphicsUtilities addRoundedRect:
CGRectMake(0.0f, 0.0f, 30.0f, 30.0f) toContext:context
withWidth:4.0f andHeight:4.0f];
CGFloat gray[4] = {tint, tint, tint, 1.0f};
CGContextSetFillColor(context, gray);
CGContextFillPath(context);
CGImageRef myRef = CGBitmapContextCreateImage (context);
free(CGBitmapContextGetData(context));
CGContextRelease(context);
UIImage *img = [UIImage imageWithCGImage:myRef];
CFRelease(myRef);
return img;
}
-(BrightnessController *) initWithBrightness: (int) aBrightness
{
self = [super init];
brightness = aBrightness;
self.title = [NSString stringWithFormat:@"%d%%", brightness * 10];
[self.tabBarItem initWithTitle:self.title image:[self
buildSwatch:(((float)brightness) / 10.0f)] tag:0];
return self;
}
- (void) loadView
{
self.view = [[[NSBundle mainBundle] loadNibNamed:@"mainview"
owner:self options:nil] lastObject];
UIView *bigSwatch = [self.view viewWithTag:101];
bigSwatch.backgroundColor = [UIColor colorWithWhite:
(brightness / 10.0f) alpha:1.0f];
}
@end
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate,
UITabBarControllerDelegate>
@end
@implementation TestBedAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSMutableArray *controllers = [NSMutableArray array];
for (int i = 0; i <= 10; i++)
{
BrightnessController *bControl = [[BrightnessController alloc]
initWithBrightness:i];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:bControl];
nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[bControl release];
[controllers addObject:nav];
[nav release];
}
// Create the toolbar and add the view controllers
UITabBarController *tbarController = [[UITabBarController alloc]
init];
tbarController.viewControllers = controllers;
tbarController.customizableViewControllers = controllers;
tbarController.delegate = self;
// Set up the window
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
[window addSubview:tbarController.view];
[window makeKeyAndVisible];
}
@end
|