Post

Replies

Boosts

Views

Activity

Issue with SwiftUI NavigationStack, Searchable Modifier, and Returning to Root View
I'm facing an issue with SwiftUI's NavigationStack when using the searchable modifier. Everything works as expected when navigating between views, but if I use the search bar to filter a list and then tap on a filtered result, I can navigate to the next view. However, in the subsequent view, my "Set and Return to Root" button, which is supposed to call popToRoot(), does not work. Here's the setup: Structure: RootView: Contains a list with items 1-7. ActivityView: Contains a list of activities that can be filtered with the searchable modifier. SettingView: Contains a button labeled "Set and Return to Root" that calls popToRoot() to navigate back to the root view. RootView struct RootView: View { @EnvironmentObject var navManager: NavigationStateManager var body: some View { NavigationStack(path: $navManager.selectionPath) { List(1...7, id: \.self) { item in Button("Element \(item)") { // Navigate to ActivityView with an example string navManager.selectionPath.append(NavigationTarget.activity) } } .navigationTitle("Root View") .navigationDestination(for: NavigationTarget.self) { destination in switch destination { case .activity: ActivityView() case .settings: SettingsView() } } } } } ActivityView struct ActivityView: View { @EnvironmentObject var navManager: NavigationStateManager let activities = ["Running", "Swimming", "Cycling", "Hiking", "Yoga", "Weightlifting", "Boxing"] @State private var searchText = "" var filteredActivities: [String] { if searchText.isEmpty { return activities } else { return activities.filter { $0.localizedCaseInsensitiveContains(searchText) } } } var body: some View { List { ForEach(filteredActivities, id: \.self) { activity in NavigationLink( destination: SettingsView(), // Navigiere zur SettingsView label: { HStack { Text(activity) .padding() Spacer() } } ) } } .navigationTitle("Choose Activity") .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: "Search Activities") } } SettingView struct SettingsView: View { @EnvironmentObject var navManager: NavigationStateManager var body: some View { VStack { Text("Settings") .font(.largeTitle) .padding() Button("Set and Return to Root") { // Pop to the root view when the button is pressed navManager.popToRoot() } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } .navigationTitle("Settings") } } NavigationStateManager // Define enum globally at the top enum NavigationTarget { case activity case settings } class NavigationStateManager: ObservableObject { @Published var selectionPath = NavigationPath() func popToRoot() { selectionPath = NavigationPath() } func popView() { selectionPath.removeLast() } } Problem When I search in the ActivityView and tap on a filtered result, I successfully navigate to the SettingView. However, in this view, pressing the "Set and Return to Root" button does not trigger the navigation back to RootView, even though popToRoot() is being called. This issue only occurs when using the search bar and filtering results. If I navigate without using the search bar, the button works as expected. Question Why is the popToRoot() function failing after a search operation, and how can I ensure that I can return to the root view after filtering the list? Any insights or suggestions would be greatly appreciated!
4
1
390
Sep ’24
How can I create a custom SignInWithAppleButton that uses only the Apple logo and implements the onRequest and onCompletion logic?
Is there a way to modify the SignInWithAppleButton so that only the Apple logo appears? According to Apple's guidelines, it's permissible to use only the icon. https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple Unfortunately, the button isn't customizable. However, I need a button that utilizes the logic of onRequest and onCompletion. Here's my current code using the standard SignInWithAppleButton. SignInWithAppleButton(.signIn, onRequest: { request in print("Apple ID Request") AppleSignInManager.shared.requestAppleAuthorization(request) }, onCompletion: { result in print("Apple ID Completion") handleAppleID(result) } ) .font(.title) .signInWithAppleButtonStyle(.white) .frame(height: 50) I tried exploring various SwiftUI customization options for the SignInWithAppleButton, such as adjusting its style or overlaying it with a custom image. I was expecting to find a way to remove the text and display only the Apple logo, as permitted by Apple's guidelines. However, I found that the SignInWithAppleButton isn't easily customizable in this way. So, I'm seeking guidance on alternative approaches to achieve the desired customization while still maintaining the functionality provided by the onRequest and onCompletion handlers.
2
0
747
May ’24
Workouts created with SingleGoalWorkout do not have a display name, as opposed to workouts created with CustomWorkout. Why is that?
With CustomWorkout, I can assign a name (displayName) to workouts, which also appears in the Workout app. Unfortunately, this parameter is missing for common workouts such as SingleGoalWorkout. Is there a reason for this? I find it inconvenient when the name is missing CustomWorkout init(activity: HKWorkoutActivityType, location: HKWorkoutSessionLocationType, displayName: String?, warmup: WorkoutStep?, blocks: [IntervalBlock], cooldown: WorkoutStep?) SingleGoalWorkout init(activity: HKWorkoutActivityType, location: HKWorkoutSessionLocationType, swimmingLocation: HKWorkoutSwimmingLocationType, goal: WorkoutGoal)
0
0
574
Dec ’23