admin管理员组文章数量:1355559
Below is my code, when I run the app and on 'Add Item' button page 2 is displayed, with TextField1 with auto-focus (Please note that toolbar above keyboard is missing). When I tap on 'Go to Page 3' and later after dismissing it, the toolbar goes away.
import SwiftUI
// MARK: - Page1: Main View with TabView
struct Page1: View {
@State private var showPage2 = false
var body: some View {
NavigationStack {
TabView {
VStack {
Button(action: {
showPage2.toggle()
}) {
Label("Add Item", systemImage: "plus.circle.fill")
.font(.title)
.padding()
}
}
.navigationTitle("Page 1")
.sheet(isPresented: $showPage2) {
Page2()
}
.tabItem {
Label("Page 1", systemImage: "house")
}
Text("Other Tab Content")
.tabItem {
Label("Tab 2", systemImage: "star")
}
}
}
}
}
// MARK: - Page2: Navigation with TextFields and Button to show Page3
struct Page2: View {
@FocusState private var focusedField: Field?
@State private var showPage3 = false
enum Field: Int {
case textField1 = 1
case textField2
case textField3
}
var body: some View {
NavigationStack {
VStack {
List {
Section {
TextField("TextField 1", text: .constant(""))
.focused($focusedField, equals: .textField1)
.textFieldStyle(.roundedBorder)
.padding()
TextField("TextField 2", text: .constant(""))
.focused($focusedField, equals: .textField2)
.textFieldStyle(.roundedBorder)
.padding()
TextField("TextField 3", text: .constant(""))
.focused($focusedField, equals: .textField3)
.textFieldStyle(.roundedBorder)
.padding()
}
}
.navigationTitle("Page 2")
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Button("Previous") {
if let current = focusedField?.rawValue, current > 1 {
focusedField = Field(rawValue: current - 1)
}
}
Button("Next") {
if let current = focusedField?.rawValue, current < 3 {
focusedField = Field(rawValue: current + 1)
}
}
Spacer()
Button("Done") {
focusedField = nil
}
}
}
Button("Go to Page 3") {
showPage3.toggle()
}
.padding()
.font(.title2)
.foregroundColor(.blue)
.sheet(isPresented: $showPage3) {
Page3()
}
}
.padding(.bottom, 100) // Adjust bottom padding to account for keyboard
}
.onAppear {
focusedField = .textField1
}
.onTapGesture {
// Dismiss keyboard when tapping outside
focusedField = nil
}
}
}
// MARK: - Page3: Simple Page with Content
struct Page3: View {
var body: some View {
NavigationStack {
VStack {
Text("Page 3")
.font(.largeTitle)
.padding()
Spacer()
}
.navigationTitle("Page 3")
}
}
}
// MARK: - Main App View
@main
struct SwiftUIApp: App {
var body: some Scene {
WindowGroup {
Page1()
}
}
}
What am I missing?
本文标签: SwiftUI Toolbar is disappearingStack Overflow
版权声明:本文标题:SwiftUI: Toolbar is disappearing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743998208a2573378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论