Search code examples
iosobjective-cuiviewcontrollerxcode6childviewcontroller

Child View Controller within Parent View Controller


I am making an app where I want to show the child view controller only within the parent view controller i.e. only in 3/4 part of the parent view controller. I have implemented the following code but the child view controller is filling up the whole parent view controller.

My code is:

- (CardsChildViewController *)viewControllerAtIndex:(NSUInteger)index {

    CardsChildViewController *childViewController = [[CardsChildViewController alloc]     initWithNibName:@"CardsChildViewController" bundle:nil];
childViewController.index = index;



    return childViewController;

}

and on viewDidLoad function I am writing:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.pageController = [[UIPageViewController alloc]         initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll     navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    CardsChildViewController *initialViewController = [self viewControllerAtIndex:0];


    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers     direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

}

I made this by taking help from : http://www.appcoda.com/uipageviewcontroller-tutorial-intro/


Solution

  • Try to change pageController's frame to the following.

    CGRect frame = self.view.frame;
    
    CGRect insetFrame = CGRectInset(frame, frame.size.width * 1/8, frame.size.height * 1/8);