How to add QLPreviewController as Subview in objective C

Naga Harish M picture Naga Harish M · Dec 13, 2011 · Viewed 9.8k times · Source

Is it possible to add QLPreviewController to UIView as sub view.

I tried like this

[self.view addSubview:previewViewController.view] 

I also called reloadData

[previewViewController reloadData];

I check with this URL Adding QLPreviewController as subview doesn't load PDF . But I did not understand what is self.pdfPreviewView

Please guide me how I can add QLPreviewController as sub view..

Answer

railwayparade picture railwayparade · Dec 13, 2011

Yes its possible, see the code below:

QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
[self addChildViewController:preview];//*view controller containment
//set the frame from the parent view
CGFloat w= self.quickLookView.frame.size.width; 
CGFloat h= self.quickLookView.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
[self.quickLookView addSubview:preview.view];    
[preview didMoveToParentViewController:self];
//save a reference to the preview controller in an ivar
self.previewController = preview;