Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting View Controller on iPad only allows .Popover

I have an iPhone application that I am trying to make universal. I have this code:

let documentMenu = UIDocumentMenuViewController(documentTypes: [kUTTypeContent as String], inMode: .Import)
documentMenu.modalPresentationStyle = .FormSheet
documentMenu.delegate = self
self.presentationContext.presentViewController(documentMenu, animated: true, completion: nil)

self.presentationContext is just a view controller passed into the class.

Every time this code is executed, this error occurs:

Your application has presented a UIDocumentMenuViewController (). In its current trait environment, the modalPresentationStyle of a UIDocumentMenuViewController with this style is UIModalPresentationPopover. You must provide location information for this popover through the view controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the view controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

I am not sure what's going on. I have even tried to set the sourceView and sourceRect, to which does stop the error, however, it sticks the DocumentMenuViewController into a popover, I do not what that. I need this modally presented in the center of the screen. Any help is appreciated.

like image 366
crizzwald Avatar asked Dec 29 '25 07:12

crizzwald


1 Answers

Following code is working fine

let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)

    importMenu.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

    importMenu.delegate = self
    importMenu.modalPresentationStyle = .formSheet
    self.present(importMenu, animated: true, completion: nil)
like image 166
Guri S Avatar answered Dec 30 '25 22:12

Guri S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!