Search code examples
iosswiftuidocumentbrowservc

How to add back or cancel button to dismiss UIDocumentBrowser


I need to present UIDocumentBrowser for uploading a document. But I am not able to place a back or cancel button in the navigation bar. The image below is a screenshot of the file browser in WhatsApp. Can anybody help me?

enter image description here


Solution

  • The UIDocumentBrowserViewController is designed to only be used as a root view controller, which is why it has no "Back" or "Cancel" button. As per the documentation:

    https://developer.apple.com/documentation/uikit/view_controllers/adding_a_document_browser_to_your_app

    Important

    Always assign the document browser as your app's root view controller. Don't place the document browser in a navigation controller, tab bar, or split view, and don't present the document browser modally.

    If you want to present a document browser from another location in your view hierarchy, use a UIDocumentPickerViewController instead.