13
votes

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..

2

2 Answers

26
votes

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;
4
votes

Swift 3.x

private var pVC: QLPreviewController?

override func viewDidLoad() {
    super.viewDidLoad()
    // I do not not why, but it needs to be setup after delay.
    DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: setupPreview)
}

private func setupPreview() {
    if (pVC != nil) { return }

    let preview = QLPreviewController()
    preview.dataSource = self
    preview.delegate = self

    preview.view.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: previewView.frame.size)
    previewView.addSubview(preview.view)

    preview.didMove(toParentViewController: self)
    pVC = preview
}