OnAppear calls unexpectedly when Keyboard Appears in SwiftUI

前端 未结 2 918
南笙
南笙 2021-02-02 12:38

I am experiencing very odd behavior in SwiftUI 2.0 and iOS14.

When the keyboard appears on the screen, the OnAppear method of other tab\'s view called automatically.

相关标签:
2条回答
  • 2021-02-02 13:07

    Having the same issue myself, I think this is a bug or something like that, however I came up with a solution maybe a workaround until apple will fix it.

    The thing that I did is basically I used a LazyVStack, and this seems to be working perfectly.

    LazyVStack {
        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)")
    })
    }
    

    Now the OnAppear method of other tab's view it is not called automatically when the keyboard appear.

    0 讨论(0)
  • 2021-02-02 13:16

    To avoid reloading your view try with on the TabView

    .ignoresSafeArea(.keyboard, edges: .bottom)
    

    It only works on iOS 14

    0 讨论(0)
提交回复
热议问题