Show default detail screen in UINavigationController

How can I show a default view when there's no selection like in a normal NavigationView but with a UINavigationController?

I've tried this
Code Block Swift
} content: {
NavigationView {
Text("Hello, World!")
.navigationTitle("Title")
Text("No selection")
}
.navigationBarHidden(true)
}

but then the search bar doesn't show.

Code Block Swift
struct SearchBarNavigationView<SearchResultsContent: View, Content: View>: UIViewControllerRepresentable {
class Coordinator: NSObject, UISearchResultsUpdating {
private let parent: SearchBarNavigationView
init(_ parent: SearchBarNavigationView) {
self.parent = parent
}
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
DispatchQueue.main.async { self.parent.text = searchText.trimmingCharacters(in: .whitespaces) }
}
}
@Binding private var text: String
private let searchResultsContent: SearchResultsContent
private let content: Content
init(text: Binding<String>, @ViewBuilder searchResultsContent: () -> SearchResultsContent, @ViewBuilder content: () -> Content) {
_text = text
self.searchResultsContent = searchResultsContent()
self.content = content()
}
func makeUIViewController(context: Context) -> UINavigationController {
let rootViewController = UIHostingController(rootView: content)
let navigationController = UINavigationController(rootViewController: rootViewController)
navigationController.navigationBar.prefersLargeTitles = true
let searchResultsController = UIHostingController(rootView: searchResultsContent)
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = context.coordinator
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.autocapitalizationType = .none
navigationController.navigationBar.topItem?.searchController = searchController
return navigationController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
if let searchResultsController = uiViewController.navigationBar.topItem?.searchController?.searchResultsController as? UIHostingController<SearchResultsContent> {
searchResultsController.rootView = searchResultsContent
searchResultsController.view.setNeedsDisplay()
}
if let rootViewController = uiViewController.topViewController as? UIHostingController<Content> {
rootViewController.rootView = content
rootViewController.view.setNeedsDisplay()
}
uiViewController.navigationBar.topItem?.searchController?.searchBar.text = text
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
}

Code Block Swift
SearchBarNavigationView(text: $searchText) {
Text(searchText)
} content: {
Text("Hello, World!")
.navigationTitle("Title")
Text("No selection") // make this show when no selection
}
.ignoresSafeArea()

Answered by BabyJ in 653315022
I think I have fixed this, without making any changes to the UINavigationController but to the view body itself.
Code Block Swift
NavigationView { // wrapping in NavigationView
SearchBarNavigationView(text: $searchText) {
Text(searchText)
} content: {
Text("Hello, World!")
.navigationTitle("Title")
}
.navigationTitle("Title") // for back button title
.navigationBarHidden(true) // hiding bar when showing main content
.ignoresSafeArea()
Text("No selection")
}


Accepted Answer
I think I have fixed this, without making any changes to the UINavigationController but to the view body itself.
Code Block Swift
NavigationView { // wrapping in NavigationView
SearchBarNavigationView(text: $searchText) {
Text(searchText)
} content: {
Text("Hello, World!")
.navigationTitle("Title")
}
.navigationTitle("Title") // for back button title
.navigationBarHidden(true) // hiding bar when showing main content
.ignoresSafeArea()
Text("No selection")
}


Show default detail screen in UINavigationController
 
 
Q