Lag When Dismissing Keyboard in ScrollView Inside NavigationStack in SwiftUI

I’m experiencing a lag in SwiftUI when dismissing the keyboard inside a ScrollView that’s within a NavigationStack. When the keyboard is opened and then dismissed, the view seems to lag as it resizes back to its original state. This issue occurs when the content is in a ScrollView.

I’m using iOS 17, and the resizing of the content feels choppy after the keyboard interaction. Here’s a simplified version of my code:

VStack {
    NavigationStack {
        ScrollView {
            createAllForm
        }
        .onAppear {
            if #available(iOS 17.0, *) {
                Self.addTripOpen.sendDonation()
            }
        }
        .toolbar {
            ToolbarItem(placement: .topBarLeading) {
                Button(action: {
                    presentationMode.wrappedValue.dismiss()
                }, label: {
                    Image(systemName: IconsEnum.closeIcon).foregroundColor(.gray)
                })
            }
        }
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                Button(LocalizedText.create, action: {
                    focusedField = nil
                    withAnimation {
                        addTripViewModel.creatingTrip = true
                        addTripViewModel.addTripToFirebase(presentationMode: presentationMode)
                    }
                }).disabled(addTripViewModel.disableCreate).foregroundColor(Color(addTripViewModel.disableCreate ? ColorsEnum.greyColor : ColorsEnum.tripBlue))
            }
        }
        .navigationTitle(LocalizedText.createTrip)
        .navigationBarTitleDisplayMode(.inline)
        .isLoadingView(isLoading: addTripViewModel.creatingTrip)
        .alert(addTripViewModel.alertMessage, isPresented: $addTripViewModel.showingAlert) {
            Button(LocalizedText.acceptLabel, role: .cancel) { }
        }
        .onDisappear {
            if addTripViewModel.isCreated {
                processCompletedCount += 1
            }
            if let currentAppVersion = Bundle.currentAppVersion,
               processCompletedCount >= 2,
               currentAppVersion != lastVersionPromptedForReview,
               addTripViewModel.isCreated {
                presentReview()
                lastVersionPromptedForReview = currentAppVersion
            }
            onDismiss(addTripViewModel.isCreated)
        }
        .sheet(isPresented: $addTripViewModel.showingAddUsers) {
            AddUsers().environmentObject(addTripViewModel)
        }
    }
}

This is the preview

@javiermelo_ Could you share details on the steps you've taken to investigate the hang ? You could take a look at Understanding hangs in your app to learn how to determine the cause for delays in user interactions by examining the main thread and the main run loop.

Do you also get the same results with just the relevant code in a small test project? If so, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

Lag When Dismissing Keyboard in ScrollView Inside NavigationStack in SwiftUI
 
 
Q