14

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

Community
  • 1
  • 1
Naga Harish M
  • 2,779
  • 2
  • 31
  • 45

2 Answers2

27

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;
railwayparade
  • 5,154
  • 1
  • 39
  • 49
  • 2
    Hi, Thank you for your reply. I am having few queries here.. Because I am iOS beginner. Your saying to create ival for previewController. What about datatype is it UIViewController/QLPreviewController? quickLookView means, give me more details pls? +1 – Naga Harish M Dec 17 '11 at 06:29
  • 1
    @NagaHarishMovva: Just create a UIViewController property named previewController in your Object - this should do the thing ;) – arnoapp Sep 02 '12 at 21:01
  • @railwayparade : It works very fine but i am facing an issue in iOS 5.0, when i rotate the view, the app crashes and it throws an execption: [QLPreviewController numberOfPreviewItems]: message sent to deallocated instance 0x9153600], please help me with this. – Jamal Zafar Mar 17 '13 at 16:58
  • 1
    This will only work correctly if the view of the parent controller is already visible on the screen. If you try to add QLPreviewController as a child controller from the parent controller's loadView method, this won't work. You'll have to call if from viewDidAppear – Philippe Leybaert May 18 '13 at 13:28
  • Ug, there's hardly a time when you should do anything in loadView. viewDidLoad is a good place for the majority of this work. Also, instead of hard coding frames, use self.previewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; or the auto layout constraint way. – TheCodingArt Dec 12 '14 at 20:05
  • In my case on iOS 10 toolbar and navigation bar is shown inside preview (UIView). How I can remove bars from the preview (UIView)? – Ramis Mar 02 '17 at 07:28
  • @Ramis : Did you find any solution to removing the bars in iOS 10? – Jacob Apr 25 '17 at 21:02
  • @Jacob I did post Swift version. – Ramis Apr 27 '17 at 12:22
4

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
}
Ramis
  • 13,985
  • 7
  • 81
  • 100