Skip to content

Instantly share code, notes, and snippets.

@janodev
Created December 25, 2022 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janodev/9d765a827ba3af407e70cc380cffeb80 to your computer and use it in GitHub Desktop.
Save janodev/9d765a827ba3af407e70cc380cffeb80 to your computer and use it in GitHub Desktop.
Custom modifier to hide the back button title.
import SwiftUI
/// Custom modifier to hide the back button title.
/// From https://stackoverflow.com/a/62854805/412916
/// Usage: `.modifier(HideBackButtonTitleModifier())`
struct HideBackButtonTitleModifier: ViewModifier {
@Environment(\.presentationMode) var presentation
@ViewBuilder @MainActor func body(content: Content) -> some View {
content
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action: { presentation.wrappedValue.dismiss() }) {
Image(systemName: "chevron.left")
.foregroundColor(.blue)
.imageScale(.large)
})
.contentShape(Rectangle())
.gesture(
DragGesture(coordinateSpace: .local)
.onEnded { value in
if value.translation.width > .zero
&& value.translation.height > -30
&& value.translation.height < 30 {
presentation.wrappedValue.dismiss()
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment