1

I'm trying to push a UIDocumentPickerViewController onto a UINavigationController's stack However, this results in what looks like two navigation bars. The top bar is the normal navigation bar of the navigation controller. Beneath that is the bar containing the document picker's Cancel and Done buttons. Clicking on the Cancel or Done buttons dismisses the entire view.

Question: How can I properly include a UIDocumentPickerViewController onto a navigation controller's stack, so that the Cancel and Done buttons appear in the navigation bar, and cause the previous and next view controllers to appear?

bright
  • 4,700
  • 1
  • 34
  • 59

1 Answers1

3

I believe you should be presenting the UIDocumentPickerViewController controller instead of pushing it onto the stack? I believe it’s supposed to be presented because it comes with its own toolbar.

Is there a specific reason it needs to be pushed?

func presentPicker() {

    var documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], inMode: UIDocumentPickerMode.Import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
    self.presentViewController(documentPicker, animated: true, completion: nil)

}

extension viewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        // handle picked Document
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        // pop view controller
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
        // handle picked Document
    }
}
tgrable
  • 1,013
  • 1
  • 7
  • 15
  • I need to push the controller to make it part of a multi-step operation, sort of like a wizard. – bright Nov 12 '19 at 16:08
  • 1
    You could try hiding your navigation controllers nav bar for that push but things might end up getting a little weird. – tgrable Nov 12 '19 at 16:17
  • Interesting... Incidentally, do the iOS docs say anything about combining (or not combining) these controllers? I couldn’t find anything. – bright Nov 12 '19 at 16:19
  • BTW, your code is just basic view controller presentation. I’d suggest removing everything apart from the first sentence, and providing supporting documentation if possible. – bright Nov 12 '19 at 16:24
  • 1
    I couldn't find any documentation saying you can't do it, I think its more a "you probably shouldn't" kind of thing. I've had to do some minor overrides for appearance with UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]) so you could look at something like that to help get you where you need. – tgrable Nov 12 '19 at 16:27