UIDocumentBrowserViewController handling back navigation

1k Views Asked by At

Apple states that the UIDocumentBrowserViewController should be the rootviewcontroller of your app. https://developer.apple.com/documentation/uikit/view_controllers/adding_a_document_browser_to_your_app

I have done it this way but I have some UX problem that I cant tackle. I cant find a good UX solution for navigating back into my App (switching back the app windows rootviewcontroller again to whatever I want).

However I have found that there are some properties where I can manipulate the appearance of the control, "Use the additionalLeadingNavigationBarButtonItems and additionalTrailingNavigationBarButtonItems methods to add buttons to the navigation bar.", but these wont add items to the "main" menu (where I can select other FileProviders, drive dropbox etc.) and where the possibility of going back would be the most clear.

Does anyone implemented, knows a good solution for adding a back button, FAB, back swipe gesture, or something for UIDocumentBrowserViewcontroller which is presented in a as the apps rootviewcontroller?

Im looking for a solution when the user ended the browsing of documents in the UIDocumentBrowserViewcontroller(copy,import etc) and wants to go back to another Viewcontroller.

1

There are 1 best solutions below

0
On

A great solution for this is to add a custom button through the additionalTrailingNavigationBarButtonItems or additionalLeadingNavigationBarButtonItems property. Example below:

let documentBrowser = UIDocumentBrowserViewController(forOpening: [.jpeg, .png])
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(didTapDocumentBrowserCancel))
documentBrowser.additionalTrailingNavigationBarButtonItems = [cancelButton]

Then create a function to close the document browser.

@objc private func didTapDocumentBrowserCancel() {
    dismiss(animated: true)
}

References: