0

I have a UIViewController that has UITabBar in it. I am trying to imitate a UITabBarController.

My question is how do I set or a UIViewController whenever a TabBarItem is selected?

I am confused as to how to put a UIViewController inside my UIViewController that is trying to imitate a UITabBarController.

Please don't ask me to use a UITabBarController

JayVDiyk
  • 4,277
  • 22
  • 70
  • 135

2 Answers2

1

You can use child view controllers to embed view controllers in other view controllers, just call this from your view controller:

YourViewController *childViewController = [[YourViewController alloc] init];

UIView *containerView = //some view in your view hierarchy
childViewController.view.frame = containerView.bounds;

[self addChildViewController: childViewController];
[containerView addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];

If you want to page between child view controllers, you can use a UIPageViewController as the root child view controller or alternatively borrow this code from the apple documentation:

- (void) cycleFromViewController: (UIViewController*) oldC
        toViewController: (UIViewController*) newC {

[oldC willMoveToParentViewController:nil];                        // 1
[self addChildViewController:newC];

newC.view.frame = [self newViewStartFrame];                       // 2
CGRect endFrame = [self oldViewEndFrame];

[self transitionFromViewController: oldC toViewController: newC   // 3
      duration: 0.25 options:0
      animations:^{
         newC.view.frame = oldC.view.frame;                       // 4
         oldC.view.frame = endFrame;
       }
       completion:^(BOOL finished) {
         [oldC removeFromParentViewController];                   // 5
         [newC didMoveToParentViewController:self];
        }];
}
Danny Bravo
  • 4,534
  • 1
  • 25
  • 43
  • Thank you. I am using '[containerView addSubview:childViewController.view];' to change the view, is it a bad practice? – JayVDiyk Aug 29 '15 at 05:01
1

(1) To learn it in detail, I would suggest you to go through:

Creating Custom Container View Controllers - https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html#//apple_ref/doc/uid/TP40007457-CH18-SW6

And

WWDC 2011 Session Video - Session 102 - Implementing UIViewController Containment.

(2) For quick learning, go through: Using Multiple ViewControllers on a Single Screen in iOS

Demo project at git: multiple-viewcontrollers

Nitesh Borad
  • 4,583
  • 36
  • 51