在 SwiftUI 中点击 TextField 时出现键盘错误

Keyboard bug when tapping on TextField in SwiftUI

我有一个包含多个视图的主视图,并根据 ObservabledObject 中变量值的变化选择一个,按下按钮时会发生变化。

当我 select 包含输入字段的视图时出现问题,在这种情况下,当我点击 TextField 时,它没有显示键盘,而是将我带回主页视图。

它只发生在设备上,不会发生在模拟器上。

不过,如果您将该特定视图(带有 TextField 的视图)设置为主视图(Switch 中的第一种情况),它会起作用。

这是 ObservableObject 代码:

class Watcher : ObservableObject {
    @Published var currentView: String = "home"
}

主视图:

import SwiftUI

struct MainView : View {
    var body: some View {
        
        GeometryReader { geometry in
            ZStack (alignment: .leading){
                HomepageView()
                    .frame(width: geometry.size.width, height: geometry.size.height)
            }
        }
    }
}

struct HomepageView : View {
    @ObservedObject var watcher = Watcher()
    
    init(){
        UITabBar.appearance().isHidden = true
        JSONHandler.fetchData(webService: "https://myWebsite.com/app/request.php") {
            print("loaded")
        }
    }
    
    var body: some View {
        ZStack {
            VStack {
                TabView {

                    switch self.watcher.currentView {
                    case "home":                                       //STARTING CASE
                        NavigationView {
                            DailyMenuView()
                                .navigationTitle("Recipes APP")
                                .navigationBarTitleDisplayMode(.inline)
                        }
                        .opacity(self.watcher.currentView == "newRecipe" ? 0 : 1)

                    case "innerMenu":
                        InnerMenuView(watcher: watcher)

                    case "members":
                        MembersView(watcher: watcher)

                    case "recipe":
                        RecipeView(watcher: watcher)

                    case "newRecipe":                                  //TextField View
                        TestView()

                    default:
                        Text("Error")
                    }
                }
            }
            
            VStack {
                Spacer()
                
                MenuTabView(watcher: watcher)
            }                
            .edgesIgnoringSafeArea(.bottom)
            
        }
        .environment(\.colorScheme, self.watcher.currentView == "home" ? .dark : .light)
        
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

最后,包含 TextField 的视图:

import SwiftUI

struct TestView: View {
    @State var text: String = ""
    
    var body: some View {
        VStack {
            Text("TEST VIEW")
                .font(.largeTitle)
                .fontWeight(.bold)
            
            Spacer()
            
            TextField("Write here", text: $text)
                .font(.title)
                .padding()
                .border(Color.red, width: 2)
            
            Spacer()
        }
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

希望你能帮我解决这个问题!

如果您需要更多信息,请问我。

您需要使用标签和选择。

TabView(selection: $watcher.currentView) { // < === Here
    
    switch self.watcher.currentView {
    case "home":                                       //STARTING CASE
        NavigationView {
            DailyMenuView()
                .navigationTitle("Recipes APP")
                .navigationBarTitleDisplayMode(.inline)
        }
        .opacity(self.watcher.currentView == "newRecipe" ? 0 : 1)
        .tag("home") // < === Here
        
    case "innerMenu":
        InnerMenuView(watcher: watcher)
            .tag("innerMenu") // < === Here
    case "members":
        MembersView(watcher: watcher)
            .tag("members") // < === Here
        
    case "recipe":
        RecipeView(watcher: watcher)
            .tag("recipe") // < === Here
    case "newRecipe":                                  //TextField View
        TestView()
            .tag("newRecipe") // < === Here
        
    default:
        Text("Error")
    }
}

我解决了搬家:

@ObservedObject var watcher = Watcher()

主视图内部如下:

struct MainView : View {
    @ObservedObject var watcher = Watcher()

    var body: some View {            
        GeometryReader { geometry in
            ZStack (alignment: .leading){
                HomepageView()
                    .frame(width: geometry.size.width, height: geometry.size.height)
            }
        }
    }
}