简体   繁体   中英

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 ie 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/

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);

Your page view controller is the child, not the view controller you create with your viewControllerAtIndex method.

The easiest way to set up a child view controller is to put a container view in your storyboard, make the child view controller a separate scene, and control-drag an embed segue from the container view to the view controller that you want to be the child.

When you do that the compiler does all the work to set up the connections to manage the child correctly. Your parent view controller's prepareForSegue method will fire when the view is first loaded and the child is installed. At that point you can hook up any outlets, delegate connections, or whatever else you might need.

Failing that, you can adjust the frame of your page view controller's view before adding it as a subview, as @ErAdhish suggests. But you will have a bunch of setup to do in order to forward housekeeping messages from the parent to the child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM