简体   繁体   中英

SwiftUI: searchable weird push animation?

I have the following code:

enum ContentViewRouter {
    case details
}


struct ContentView: View {
    
    var body: some View {
        
        NavigationStack {
            ZStack {
                NavigationLink(value: ContentViewRouter.details) {
                    Text("Push")
                }
            }
            .navigationDestination(for: ContentViewRouter.self) { destiantion in
                switch destiantion {
                case .details:
                    DetailsView()
                }
            }
            .navigationTitle("TEST")
        }
    }
    
}

struct DetailsView: View {
    
    @State var query = ""
    
    var body: some View {
        
        Form {
            Section("TEST") {
                Text("DETAILS")
            }
        }
        .searchable(text: $query)
        .navigationTitle("DETAILS")
        
    }
    
}

When pushing the details view it creates this animation:

在此处输入图像描述

I want to get rid of this weird looking animation "glitch":

在此处输入图像描述

How can I get the push animation to come in smooth where the search bar doesn't appear for a split second and then go away? Seems like it's a bug or I am doing it wrong?

The problem of that I think because you have .searchable which will create search bar and currently conflict with your .navigationTitle which both on the top. Maybe the search bar don't know where to put it because of the .navigationTitle

Update: After doing some research, I see that maybe the behavior maybe wrong in search bar .automatic

There are three ways to workaround

  1. If you want to keep your search bar then make NavigationView to split define navigationTitle and search bar
struct DetailsView: View {
    @State var query = ""
    
    var body: some View {
        NavigationView {
            Form {
                Section("TEST") {
                    Text("DETAILS")
                }
            }
            .searchable(text: $query)
        }
        .navigationTitle("DETAILS")
        
    }
}
  1. Keep the search bar always appear
struct DetailsView: View {
    @State var query = ""
    var body: some View {
        
        Form {
            Section("TEST") {
                Text("DETAILS")
            }
        }
        .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always))
        .navigationTitle("DETAILS")
    }
}
  1. If you want to completely remove search bar
struct DetailsView: View {
    
    @State var query = ""
    
    var body: some View {
        
        Form {
            Section("TEST") {
                Text("DETAILS")
            }
        }
        //.searchable(text: $query) // remove this
        .navigationTitle("DETAILS")
        
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM