swiftuiswiftui-navigationstack

SwiftUI, NavigationStack Title is duplicated during Swipe Down Gesture


I'm following Apple's build your workout app tutorial. Due to the deprecated usage of NavigationLink(_:destination:tag:selection) signature, I'm trying to change it to NavigationStack together with .NavigationDestination modifier. I did achieve most part, however, the issue is that, during the navigatorStack for the three buttons, the title got duplicated during swipe down gesture, as shown below:

enter image description here

Note that this issue happens in both simulator and physical device, but not in the preview within Xcode.

Here's the simplest self-contained code (2 files in total). The first swiftUI file:

import SwiftUI
import Foundation
import HealthKit

struct sp_7_Navigate3: View {
    @EnvironmentObject var workoutManager: sp_7_WorkoutManager
    var workoutTypes: [HKWorkoutActivityType] = [.cycling, .running, .walking]
    
    var body: some View {
        NavigationStack {
            List(workoutTypes, id: \.self) { workoutType in
                Button(action: {
                    workoutManager.selectedWorkout = workoutType
                }) {
                    Text(workoutType.name+"_\(workoutManager.selectedWorkout)")
                }
            }
            .navigationTitle("Workouts_s")
            .navigationDestination(isPresented: Binding<Bool>(
                get: { workoutManager.selectedWorkout != nil },
                set: { if !$0 { workoutManager.selectedWorkout = nil } }
            )) {
                Text(Image(systemName: "heart"))
            }
        }
    }
}

#Preview {
    sp_7_Navigate3()
        .environmentObject(sp_7_WorkoutManager())
}

class sp_7_WorkoutManager: NSObject, ObservableObject {
    @Published var selectedWorkout: HKWorkoutActivityType? {
        didSet {
            print("inside didSet: \(selectedWorkout?.name ?? "nil")" )
            guard let selectedWorkout = selectedWorkout else { return }
            
        }
    }
}

Then the 2nd file, for main entrance:

import SwiftUI

@main
struct WorkoutAppTutorial_Watch_AppApp: App {
    @StateObject var workoutManager = sp_7_WorkoutManager()
    
    var body: some Scene {
        WindowGroup {
            NavigationView {
                sp_7_Navigate3()
            }
            .environmentObject(workoutManager)
        }
        
    }
}

Solution

  • I realize, changing from:

    @main
    struct WorkoutAppTutorial_Watch_AppApp: App {
        @StateObject var workoutManager = sp_7_WorkoutManager()
    
        var body: some Scene {
            WindowGroup {
                NavigationView {
                    sp_7_Navigate3()
                }
                .environmentObject(workoutManager)
            }
    
        }
    }
    

    to:

    @main
    struct WorkoutAppTutorial_Watch_AppApp: App {
        @StateObject var workoutManager = WorkoutManager()   
            WindowGroup {
                sp_7_Navigate3()
                    .environmentObject(workoutManager)
            }
        }
    }
    

    solve the issue. Previously, the combo NavigationView + List + NavigationLink(_:destination:tag:selection) works well, but after changing to NavigationStack, the parent NavigationView is no longer needed.