SwiftUI: Toolbar is disappearing - Stack Overflow

Below is my code, when I run the app and on 'Add Item' button page 2 is displayed, with TextF

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743998209a4541027.html

相关推荐

  • SwiftUI: Toolbar is disappearing - Stack Overflow

    Below is my code, when I run the app and on 'Add Item' button page 2 is displayed, with TextF

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信