Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController inside another UIViewController, is it possible?

I'm making a newsstand app and I need multiple components in a single view. I have a UIViewController that displays and manages a pdf (zooming, scrolling,etc...) but I don't want it to be full screen instead I want it to be like an UIView inside another UIViewController which has a menu on top, page numbering on the bottom and other tools. Both are subclass of UIViewController... I already have both working separately. I just want the pdf viewer inside the other. Is it possible? here's some code of what I want (lets say is inside the viewDidLoad)... but obviously doesn't work. Any help is appreciated. It also must be IOS 5.1.1 compatible and above

    NSString *path = [[NSBundle mainBundle] pathForResource:@"myPdf" ofType:@"pdf"];
    PDFViewController *page = [[PDFViewController alloc] initWithPDFAtPath:path];
    //pdfView is a UIView of size 600x600 right in the middle
    [self setPdfView:[page view]]; //this doest work... but it's basically what I want instead of presenting a viewController
    //[self presentViewController:page animated:YES completion:NULL];

I'm also aware of this, but I don't understand... if this is the way I would like some guidance:

addChildViewController:
willMoveToParentViewController:
didMoveToParentViewController:
like image 570
user2387149 Avatar asked Feb 07 '14 00:02

user2387149


People also ask

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

What is the role of UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

Which method is called first in ViewController?

init method does not invoke the loadView method. This one is invoked the very first time the ViewController is asked to show its view to the screen. If the VC is presented and the view has never been loaded before, the loadView method is invoked.


1 Answers

As of iOS 5 you can do View Controller Containment which lets you add child view controllers into a parent view controller. The child view controllers can manage all the logic like they normally would but the view of the child controller can match any frame/position you like.

To add the controller you can do

   [self addChildViewController:childViewController];             
   childViewController.view.frame = [self frameForChildController];
   [self.view addSubview:childViewController.view];
   [childViewController didMoveToParentViewController:self];

You can learn more about it in the View Controller Documentation

like image 148
GracelessROB Avatar answered Sep 29 '22 18:09

GracelessROB