I experienced rendering problems when using animations inside a NavigationView. The same animations work fine when no NavigationView is present.
See my example code below. There are two previews, one working, the other one not. Unfortunately, this behavior is not limited to previews but also happens when the app runs in simulator or on a real device.
Am I doing something wrong? Any help would be appreciated. Thanks!
import SwiftUI
struct MyView: View {
@State private var isAnimating = false
var body: some View {
let animation = Animation
.linear
.repeatForever(autoreverses: false)
return Image(systemName: "iphone")
.rotationEffect(.degrees(isAnimating ? 360 : 0))
.animation(animation)
.onAppear { isAnimating = true }
}
}
struct MyView_Previews: PreviewProvider {
static var previews: some View {
Group {
MyView()
.previewDisplayName("Working")
NavigationView {
MyView()
}
.previewDisplayName("Not Working")
}
}
}
(Xcode12.5, iOS14.5)
Post
Replies
Boosts
Views
Activity
Hi everyone,
I have a simple setup of two views. Tapping a button on the first view, opens the second view in a NavigationView. I want to hide the NavigationBar on the first view and show it on the second. So far so good.
It can happen that the first view updates during the animation of one view to the next. When this happens, the NavigationBar is also hidden on the second view.
I put together this code. You can copy-paste it to an empty project's ContentView-file to see what I mean.
Does anyone have a good idea of how to fix it?
import SwiftUI
struct ContentView: View {
@State var showsExtraText = false
@State var showsNextScreen = false
var body: some View {
NavigationView {
VStack {
NavigationLink(
destination:
Text("Destination")
.navigationTitle("Hallo")
,
isActive: $showsNextScreen,
label: {
Button("Press me") {
showsNextScreen = true
// The following lines are for demonstration purposes
// I the real app, the update is caused by a backend call finishing.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.showsExtraText.toggle()
}
}
})
if showsExtraText {
Text("Extra text")
}
}
.navigationBarHidden(true)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}