I'm trying to set toolbarBackground visibile with animation, but it's happen immediatly. Are there ways to make this with animation?
struct MyView: View {
@State private var barHidden = true
var body: some View {
NavigationStack {
Button("action") {
barHidden.toggle()
}
.animation(.easeInOut, value: barHidden)
.toolbar {
ToolbarItem(placement: .principal) {
Text("Title")
}
}
.toolbarBackground(Color.red, for: .navigationBar)
.toolbarBackground(barHidden ? .hidden : .visible, for: .navigationBar)
}
}
}
All you need to do is take advantage of the
safeAreaInsetsmodifier to simulate the animation of the toolbar background. Here's what I've done:safeAreasize using aGeomtryReadersafeAreaInsetsmodifier to theNavigationStackto place a View where the toolbar is supposed to be, which, in this case, will beRectangleoverlayto aforementionedRectanglethat will be our titleHere's the code:
Here's the result: