How to add QLPreviewController as Subview in objective C
Asked Answered
H

2

15

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

Hunley answered 13/12, 2011 at 17:24 Comment(1)
as of ios6 this is obsolete: see oleb.net/blog/2012/10/remote-view-controllers-in-ios-6Gargantuan
D
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;
Darrondarrow answered 13/12, 2011 at 21:15 Comment(8)
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? +1Hunley
@NagaHarishMovva: Just create a UIViewController property named previewController in your Object - this should do the thing ;)Narthex
@Darrondarrow : 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.Lordinwaiting
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 viewDidAppearOlla
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.Jemine
In my case on iOS 10 toolbar and navigation bar is shown inside preview (UIView). How I can remove bars from the preview (UIView)?Cowey
@Cowey : Did you find any solution to removing the bars in iOS 10?Educatory
@Educatory I did post Swift version.Cowey
C
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
}
Cowey answered 27/4, 2017 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.