QLPreviewController toolbar not appearing

swift

269 просмотра

1 ответ

266 Репутация автора

I am using QLPreviewController to display a PDF. Now I am trying to add button items to the toolbar. When I print the toolbarItems I can see they are being added but my toolbar will not appear. Why is it not appearing?

func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {


        let rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(drawCircle))

        let rightButton2 = UIBarButtonItem(title: "Title2", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(drawCircle))

        controller.setToolbarItems([rightButton, rightButton2], animated: false)

        print(controller.toolbarItems)

        let localUrl = String(format:"%@/%@", PDFFilePath, fileNameGroup)
        let url = NSURL.fileURLWithPath(localUrl)

        return url
    }

viewPDF = QLPreviewController()

        viewPDF.dataSource = self


        self.presentViewController(viewPDF, animated: true, completion: nil)
Автор: user979331 Источник Размещён: 17.07.2016 02:16

Ответы (1)


0 плюса

1713 Репутация автора

From what I infered, I am not sure that you are adding the buttons to the right position. But the problem is for that you need to make a custom view controller and add QLPreviewController as a subview. Something like this:

class CustomQLViewController: UIViewController {

    var customQL=QLPreviewController()
    @IBOutlet weak var qlView: UIView! //defined in storyboard for QLPreviewController

    override dynamic func viewDidLoad() {
        super.viewDidLoad()
        customQL.dataSource = self
        customQL.view.frame = CGRectMake(0, 0, qlView.frame.size.width, qlView.frame.size.height)
        qlView.addSubview(customQL.view)
    }

What you see here is a view controller added in the storyboard with a custom UIView, used to represent the region that will be used up by QLPreviewController. Just add the buttons you want in the storyboard and connect their outlets in the class. The main thing here is to set the QLPreview controller to the dimensions of the view you added in the storyboard (ie. qlView) and add set the frame of the QLPreview object to qlView. The final step would be to add the QLpreview as a subview in qlView.

Автор: Umar Farooque Размещён: 29.07.2016 07:10
Вопросы из категории :
32x32