UIKit Document App -- adding a toolbar

I've had to ditch the SwiftUI app lifecycle due to this issue: https://developer.apple.com/forums/thread/742580

After creating a new UIKit document app as a test, it doesn't have a toolbar when opening the document. How can I add one along the lines of https://developer.apple.com/wwdc22/10069 ?

The UIDocumentViewController isn't already embedded in a UINavigationController it seems.

To reproduce: New Project -> iOS -> Document App. Select Interface: Storyboard. Add an empty "untitled.txt" resource to the project. Change the first line in documentBrowser(_:,didRequestDocumentCreationWithHandler:) to

let newDocumentURL: URL? = Bundle.main.url(forResource: "untitled", withExtension: "txt")
Add a Comment

Replies

Ok seems I have to change presentDocument to:

    func presentDocument(at documentURL: URL) {

        let navVC = UINavigationController()

        let doc = Document(fileURL: documentURL)
        let docVC = UIDocumentViewController(document: doc)
        navVC.modalPresentationStyle = .fullScreen
        navVC.addChild(docVC)

        present(navVC, animated: true, completion: nil)
    }

So embed the UIDocumentViewController inside a UINavigationController.

  • You should not use addChild to add children to a UINavigationController – it expects children to always be added via its api (setViewControllers() or pushViewController()). In this case the simplest thing would be to use UINavigationController(rootViewController:) to create the navigation controller with your UIDocumentViewController as its root.

Add a Comment