简体   繁体   中英

How to add QLPreviewController as Subview in objective C

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

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;

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM