swiftuiuitabbarcontrollertabbar

Animating `TabBar` visibility transitions in SwiftUI


I have a tabbed view which at some point pushes a full screen view to show some screenshots; for this specific screen I'm hiding the TabBar using the modifier.-

.toolbar(.hidden, for: .tabBar)

Now, when I pop back, the TabBar reappears in a non graceful way (it just appears with no animation). In SwiftUI I typically create animations with the withAnimation operator, but in this case I have no place to use unless I override the back button to handle the event myself (still not sure if this would work anyway)

Is there any way to automatically force the animation to hide / show the TabBar as in the traditional UIKit way?:

self.navigationController.setToolbarHidden(false, animated: true)

Solution

  • You can depend the visibility to a simple State variable and toggle it with animation like:

    @State private var isVisible = true
    ,,,
    
    .toolbar(isVisible ? .automatic : .hidden, for: .tabBar)
    .animation(.default, value: isVisible)
    

    Demo