-
Notifications
You must be signed in to change notification settings - Fork 111
Containers UITabBarController example
In this example, we show how HLSPlaceholderViewController
can be used to implement a UITabBarController
-like container. The final result will look as follows:
The transition between pages is achieved using one of the built-in CoconutKit 3D-flip transitions:
- Create a new Xcode project and add CoconutKit to it
- Create an
HLSPlaceholderViewController
subclass calledMyTabBarController
. We will manage the view layout using Interface Builder, you should therefore add axib
file bearing the class name at the same time (simply check the With XIB for user interface checkbox when creating the class files)
```objective-c
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *placeholderView = [self placeholderViewAtIndex:0];
placeholderView.backgroundColor = self.view.backgroundColor;
}
```
* A segmented control to change which view controller is displayed by the container
-
We need to implement a method which will be responsible of setting the view controller to be displayed in the placeholder view. This is achieved by calling one of the
-[HLSPlaceholderViewController setInsetViewController...]
methods, using a flip transition animation class:- (void)displayPage:(NSInteger)page animated:(BOOL)animated { Class transitionClass = animated ? [HLSTransitionFlipHorizontally class] : [HLSTransitionNone class]; UIViewController *viewController = nil; if (page == 0) { viewController = [[FirstViewController alloc] init]; } else if (page == 1) { viewController = [[SecondViewController alloc] init]; } else { return; } [self setInsetViewController:viewController atIndex:0 withTransitionClass:transitionClass]; }
We call this method in the
-viewDidLoad
method to initially install the first view controller (without animation), as well as in a-changePage:
action method we bind to the segmented control:- (void)viewDidLoad { [super viewDidLoad]; UIView *placeholderView = [self placeholderViewAtIndex:0]; placeholderView.backgroundColor = self.view.backgroundColor; [self displayPage:0 animated:NO]; } - (IBAction)changePage:(id)sender { UISegmentedControl *segmentedControl = sender; [self displayPage:segmentedControl.selectedSegmentIndex animated:YES]; }
-
Edit both
FirstViewController
andSecondViewController
xib
files so that their view sizes match the one ofMyTabBarController
placeholder view. Even ifHLSPlaceholderViewController
resizes the frame of its child view controllers to match the placeholder view bounds, it is better to design at the proper scale. Also add two different labels1
and2
in the middle of those views, with proper autoresizing mask (flexible height and width)
```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[MyTabBarController alloc] init];
return YES;
}
```
- Build and run the application. Try to rotate the view and to switch between pages. If your autoresizing masks were set up properly, the labels should behave properly when rotating. Congratulations! You have implemented your own
UITabBarController
-like container!