How to add QLPreviewController as Subview in objective C

 ̄綄美尐妖づ 提交于 2019-11-30 02:09:00

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!