Mal*_*oni 24 keyboard textfield tabview swiftui swiftui-tabview
我在 SwiftUI 2.0 和 iOS14 中遇到了非常奇怪的行为。
当键盘出现在屏幕上时,会自动调用其他选项卡视图的 OnAppear 方法。
但是,这很好用 Xcode 11.7
这是产生上述错误的代码。
struct ContentView: View {
var body: some View {
TabView {
DemoView(screenName: "Home")
.tabItem {
Image.init(systemName: "star.fill")
Text("Home")
}
DemoView(screenName: "Result")
.tabItem {
Image.init(systemName: "star.fill")
Text("Result")
}
DemoView(screenName: "More")
.tabItem {
Image.init(systemName: "star.fill")
Text("More")
}
}
}
}
struct DemoView:View {
@State var text:String = ""
var screenName:String
var body: some View {
VStack{
Text(screenName)
.font(.title)
TextField("Buggy Keyboard Issue", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text("Issue : When keyboard appears, onAppear of other 2 tabs call automatically.")
.font(.footnote)
}
.padding()
.onAppear(perform: {
debugPrint("OnAppear of : \(screenName)")
})
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是 SwiftUI 2.0 的一个错误,但不确定。任何帮助将不胜感激。
谢谢
小智 0
为了避免重新加载视图,请尝试在 TabView 上使用
.ignoresSafeArea(.keyboard, edges: .bottom)
Run Code Online (Sandbox Code Playgroud)
仅适用于 iOS 14
| 归档时间: |
|
| 查看次数: |
1102 次 |
| 最近记录: |