UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Root view content update with transition doesn't work

Forums > SwiftUI

Hello fellow SwiftUI'ers! 🤓

In my project I have the root view which conditionally displays either full-screen logo image (loading mode), or main view of the app, based on whether required data was loaded.

When root view appears, it initiates data loading via onReceive and updates local @State property with received data. That triggers root view update and the main view displayed instead.

I can't make main view to appear with slide transition (or any other transition, that is) - no matter how I do it. I have added .transition(.slide) to both of my views and enclosed @State property update in withAnimation, but still the main view always appears immediately. 😕

I have tried moving .transition modifier around, using DispatchQueue.main.async to update @State property, enclosing my views in different type of container - absolutely nothing does any difference. I bet I'm missing something fundamental here and would be very grateful of any kind of help! 🙏

Thanks a lot!

Here is greatly simplified version of my root view.

struct RootView: View {
    @State private var data: [String]

    var body: some View {
        return Group {
            Group {
                if !data.isEmpty {
                    NavigationView {
                        MainView(with: data)
                    }.transition(.slide)
                } else {
                    ZStack {
                        Color.clear
                        Image(uiImage: Images.oxygenLogo.image)
                    }.transition(.slide)
                }
            }

        }.onReceive(service.loadData().receive(on: DispatchQueue.main)) { data in
            withAnimation {
                self.data = data
            }
        }
    }
}

2      

After trial-and-error session I finally found what was causing transitions to not work. It's root Group... Changing it to ZStack immediatelly resolved the issue. I was able to make transition work by altering other views, but the animation duration parameter was always ignored nevertheless. With the ZStack as root view both transitions and animation duration works as expected. Hope that will help someone 🤓

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.