My project is having this weird bug with NavigationLink and withAnimation(). Where animation of NavigationLink, changes the view on the screen it is moving away from (see video). The NavigationLink is in one viewController (MainView) And the NavigationLink gets triggered from another view (MainViewPostView), via a Binding. Maybe this is part of the problem. Is this just a bug, or can you do something?
stuct MainView: View {
var body: some View {
mainView
}
private var mainView: some View {
VStack {
VStack {
if let p = post {
NavigationLink(destination: CommentView(post: p), isActive: $navigationViewIsActive){ EmptyView() }
}
}.hidden()
ForEach(postViewModel.posts, id: \.id) { post in
MainVIewPostView(post: post, navigateToComment: $navigationViewIsActive, selectedPost: $post)
}
}
}
}
struct MainVIewPostView: View {
@ObservedObject var postRowViewModel: PostRowViewModel
private var mainView = MainView()
@Binding var navigationViewIsActive: Bool
@Binding var selectedPost: Post?
init(post: Post, navigateToComment: Binding<Bool>, selectedPost: Binding<Post?>) {
self.postRowViewModel = PostRowViewModel(post: post)
self._navigationViewIsActive = navigateToComment
self._selectedPost = selectedPost
}
var body: some View {
VStack {
cellView
}
}
var cellView: some View {
Button {
selectedPost = postRowViewModel.post
withAnimation(.easeInOut(duration: 1)) {
navigationViewIsActive.toggle()
}
} label: {
Image(systemName: "message")
.font(.system(size: 26))
.foregroundColor(.black)
Text("24")
.foregroundColor(.black)
}
}
}