Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created February 9, 2023 18:49
Show Gist options
  • Save DarrenHurst/dbf2d1682392825f0b2e95a678164908 to your computer and use it in GitHub Desktop.
Save DarrenHurst/dbf2d1682392825f0b2e95a678164908 to your computer and use it in GitHub Desktop.
BackButton Control, installs as modifier .backButton()
import Foundation
import SwiftUI
struct BackButton: View {
var dismissAction: () -> Void
var btnBack : some View { Button(action: {
self.dismissAction()
}) {
HStack {
Image(systemName: "chevron.left.circle")
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
Text("Back")
}.padding(10).overlay{
}
//.border(.bar, width: 2)
.foregroundColor(.black)
}
}
var body: some View {
VStack {
AnyView {
btnBack
}
}
}
}
struct NavigationBack: ViewModifier {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
func body(content: Content) -> some View {
content
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: BackButton(dismissAction: {
self.presentationMode.wrappedValue.dismiss()
}).btnBack)
}
}
extension View {
// install on view with .backButton()
func backButton() -> some View {
modifier(NavigationBack())
}
}
@DarrenHurst
Copy link
Author

Screenshot 2023-02-09 at 1 46 40 PM

@DarrenHurst
Copy link
Author

Your page view container should have .backButton() ie. ScrollView { content }.backButton()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment