0

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/

Adhish
  • 134
  • 8

2 Answers2

0

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);
gabbler
  • 13,626
  • 4
  • 32
  • 44
  • Can you explain or give me the link that how it exactly worked? – Adhish Oct 26 '14 at 14:16
  • Here is the [CGRect](https://developer.apple.com/library/mac/Documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGRectInset), the above code shrink a CGRect frame by a certain value horizontally and vertically, the value is 1/4 of the frame width or height. Notice positive values will decrease the size evenly for the new rectangle to be centered in the old one, while negative values will increase the size. – gabbler Oct 26 '14 at 14:19
0

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272