3

Interaction with an input field in a view inside my TabView, is causing my other views to animate weirdly when they appear. In addition, the nav bar doesn't hide as it should.

I've established it is an input field + TabView problem because:

  • This issue does not occur on the simulator (the keyboard doesn't push up).
  • It does not occur if the user navigates to the other tab views before interacting with an input field.
  • It doesn't occur when I put a tab view view's content in another view and navigate to it.

I've looked into a few solutions but had no luck: Is there a way to override the keyboard animation? Is there a way to force the tab view views to "appear" when the tab view loads? Is there a way to stop other TabView sub views being impacted by changes in other TabView views

Tab iew Code

        TabView (){
        ViewA()
            .tabItem {
                Image("viewa").renderingMode(.template)
                  Text("viewa")
            }

        ViewB()
            .tabItem {
                  Image("viewb").renderingMode(.template)
                    Text("viewb")
            }
        ViewC()
            .tabItem {
              Image("viewc").renderingMode(.template)
                Text("viewc")
             
            }
        ViewD()
            .tabItem {
                Image("viewd").renderingMode(.template)
                  Text("viewd")
            }

    }

In the tab views child views, animation is applied to a VStack containing my items like so.

VStack{
items...
}.animation(.easeInOut)

This code is used to hide the nav:

        .background(NavigationConfigurator { nav in 
        nav.hidesBarsOnSwipe = true
    })

Expected behaviour:

enter image description here

Behaviour when interacting with an input field:

enter image description here

rexr
  • 89
  • 11
  • I've done some more troubleshooting. When interacting with the input field, the onAppear method for all the other views in the tab view are triggered at this point. Why is interacting with a textfield within a view causing all the other views in a tab view to appear? – rexr Apr 23 '21 at 23:01

2 Answers2

3

It appears this is an ongoing bug with TabView and the keyboard. The temporary fix until this is resolved is converting the VStacks to LazyVStacks so that the views do not load until necessary.

See: OnAppear calls unexpectedly when Keyboard Appears in SwiftUI

rexr
  • 89
  • 11
2

There is a modifier you can use to fix this that is supported. try using this:

.ignoresSafeArea(.keyboard)

this will keep anything from moving out of the keyboard's safe area while it is moving.

Dharman
  • 30,962
  • 25
  • 85
  • 135
a1cd
  • 17,884
  • 4
  • 8
  • 28