I'm trying to display a toolbar with a done button on the keyboard. It works in isolation, but within the context of the app, the toolbar doesn't display.
The app has Home Screen View with buttons for navigation to other Views within the app, and a controller to keep track of the current view. Each section of the app has its own Navigation Stack. If I skip the main menu, the keyboard toolbar displays, but if I go through the main menu view first, it doesn't.
This seems like a bug in SwiftUI to me, but maybe there's something else going on that I'm missing.
Any help is appreciated.
Here's an extremely simplified example:
import SwiftUI
@Observable class RootViewController: CustomStringConvertible {
var description: String {
return switch(currentView) {
case .home: "Home"
case .formView: "Form View"
}
}
enum Views {
case home
case formView
}
public var currentView: Views = .home // This prevents the toolbar from displaying. Change to .formView, and it works.
}
struct ContentView: View {
@State private var rootViewController: RootViewController
init(rootViewController: RootViewController) {
self.rootViewController = rootViewController
}
var body: some View {
switch rootViewController.currentView {
case .formView:
FormView()
case .home:
HomeView(rootViewController)
}
}
}
struct FormView: View {
@State private var text: String = ""
@FocusState private var isInputActive: Bool
var body: some View {
NavigationStack {
Form {
TextField("Text", text: $text)
.keyboardType(.decimalPad)
.focused($isInputActive)
Text("Text: \(text)")
}
.navigationTitle("Keyboard Toolbar Test")
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Close") { isInputActive = false }
}
}
.padding()
}
}
}
struct HomeView: View {
@State private var rootViewController: RootViewController
init(_ rootViewController: RootViewController) {
self.rootViewController = rootViewController
}
var body: some View {
Button("Open Form") {
rootViewController.currentView = .formView
}
}
}
#Preview {
ContentView(rootViewController: RootViewController())
}