Sunday 8 November 2020

SwiftUI inconsistent behavior "pushing" to UIKit ViewController from SwiftUI View

I'm implementing some SwiftUI views into an existing UIKit App. The DashboardView (below) is a home screen, and then I need to push to existing UIKit ViewControllers. I'm trying to push the View Controllers onto a nav stack (i.e. with a back button, as opposed to modally). The below code, the push under the Show More button works as expected to push to SkateListViewController with a back button, however the the push in the WorkoutListView presents the View Controller as a modal? Does it have to do with WorkoutListView being embedded in a parent view?

struct DashboardView: View {
    var body: some View {
        ScrollView {
                VStack(spacing: 7.0) {
                    HStack {
                        Text("Workouts")
                            .font(.custom(futuraBold, size: largeTitleTextSize))
                            .padding(.leading)
                        Spacer()
                        
                        Button(action: {
                        
                            let storyBoard: UIStoryboard =  UIStoryboard(name: "Version3", bundle: nil)
                            let skateListTVC = storyBoard.instantiateViewController(withIdentifier: "skateListVC") as! SkateListTableViewController
                            UIApplication.topViewController()?.navigationController?.pushViewController(skateListTVC, animated: true) //This line works as expected and pushes onto Nav Stack with back button
                        }) {
                            Text("Show More")
                              .font(.custom(futuraMedium, size: headlineTextSize))
                                .foregroundColor(Color.blue)
                        }
                    }
                    ZStack {
                        VStack(alignment: .leading) {
                            WorkoutListView(workouts: [MockWorkout().getMockWorkout()])
                        }
                        .padding(.horizontal)
                    }
                }
               }
          }
    }    
}


struct WorkoutListView: View {
    
    @State var workouts: [HKWorkout]
    
    var body: some View {
        VStack(alignment: .leading) {
                ForEach(workouts) { workout in
                    WorkoutRow(workout: workout)
                        .onTapGesture {
                            SelectedWorkoutSingleton.sharedInstance.selectedWorkout = workout
                            let storyBoard: UIStoryboard =  UIStoryboard(name: "Version3", bundle: nil)
                                let skateDetailHostingControllerView = storyBoard.instantiateViewController(withIdentifier: "skateDetailHostingController") as! SkateDetailHostingController
                                 UIApplication.topViewController()?.navigationController?.pushViewController(skateDetailHostingControllerView, animated: true) //This line shows Hosting Controller as Modal, doesn't push
                           
                        }
                }
        }
    }
}


from SwiftUI inconsistent behavior "pushing" to UIKit ViewController from SwiftUI View

No comments:

Post a Comment