SwiftUI: Trigger view update after user pans the map
For being new to Swift/SwiftUI this is quite cool!
You want to refetch when the coordinates of your Map change. So you could use .onChange
, e.g.on the ZStack
of the Map:
.onChange(of: viewModel.mapRegion.center.latitude) { _ in
Task {
await fetchNearbyLandmarks(lat: viewModel.mapRegion.center.latitude, lon: viewModel.mapRegion.center.longitude)
}
}
Generally this works, but it might be fetching too often (on every change of latitude). So you might want to add some kind of delay, or try to figure out when the drag ended.
Custom back button for NavigationView's navigation bar in SwiftUI
TL;DR
Use this to transition to your view:
NavigationLink(destination: SampleDetails()) {}
Add this to the view itself:
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
Then, in a button action or something, dismiss the view:
presentationMode.wrappedValue.dismiss()
Full code
From a parent, navigate using NavigationLink
NavigationLink(destination: SampleDetails()) {}
In DetailsView hide navigationBarBackButton
and set custom back button to leading navigationBarItem
,
struct SampleDetails: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var btnBack : some View { Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image("ic_back") // set image here
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
Text("Go back")
}
}
}
var body: some View {
List {
Text("sample code")
}
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: btnBack)
}
}
Related Topics
How to Add an Uicollectionviewlayout Programmatically
Is It Safe to Force Unwrap an Optional After Checking It Is Not Nil
Swift Ui: Center Text into Circle
Swift Combine: What Are Those Multicast Functions for and How to Use Them
Best Way to Avoid Capturing a Copy of the Value in Closure
How to Respond with an Image Using Vapor
How to Access a Custom Function from Any File in the Same Swift Project
Video Upload to Amazon S3 in Swift
Sktexture Nearest Filtering Mode Doesn't Work (Making Pixel Art)
Swiftui View and Uihostingcontroller in Uiscrollview Breaks Scrolling
Trying to Add a Protocol to a Class Signature in Swift
Why Are Objects in the Same Sknode Layer Not Interacting with Each Other
How to Check If Dark Appearance Is Enabled Tvos
Menu Items Disabled in MACos Menubar App
How to Use Nsvisualeffectview to Blend Window with Background