如何在目标C中添加QLPreviewController作为子视图

时间:2011-12-13 17:24:35

标签: objective-c ios uiview qlpreviewcontroller

是否可以将QLPreviewController作为子视图添加到UIView。

我试过这个

[self.view addSubview:previewViewController.view] 

我也打电话给reloadData

[previewViewController reloadData];

我查看此网址Adding QLPreviewController as subview doesn't load PDF。但我不明白什么是self.pdfPreviewView

请指导我如何将QLPreviewController添加为子视图..

2 个答案:

答案 0 :(得分:25)

是的,可以参考下面的代码:

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;

答案 1 :(得分:3)

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
}