this is my code I need my toolBar buttons to navigate to other SwiftUi views
what should I write in button action { } to open the other SwiftUi View
// SwiftLoginView.swift
// Login FireBase
//
// Created by Makwin Santhosh K on 08/11/22.
//
import SwiftUI
import MapKit
import CoreLocationUI
struct OverallView: View {
var body: some View{
NavigationView {
ZStack{
ToolBarShape()
.frame(width: 400,height: 110)
.foregroundColor(.white)
.shadow(radius: 6)
.offset(x : 0, y: 410)
.toolbar {
ToolbarItemGroup(placement: .bottomBar){
//MARK: Button Home
Button(action :{
}, label:{
VStack {
Image(systemName: "house")
Text("Home")
}
.font(.footnote)
.foregroundColor(.black)
})
Spacer()
//MARK: Button Money
Button(action :{
},label:{
VStack {
Image(systemName: "dollarsign")
Text("Money")
}
.font(.footnote)
.foregroundColor(.black)
})
Spacer()
//MARK: Button Home
Button(action :{
},label:{
VStack {
Image(systemName: "person")
Text("Help")
}
.font(.footnote)
.foregroundColor(.black)
})
//MARK: Button Home
Spacer()
Button(action :{
},label:{
VStack {
Image(systemName: "menubar.rectangle")
Text("More")
}
.font(.footnote)
.foregroundColor(.black)
})
}
}
}
}
}
}
struct MapUView: View {
@StateObject public var ViewModel = ContentViewModal()
var body: some View {
ZStack(alignment: .bottom) {
AreaMap(region: $ViewModel.region)
LocationButton(.currentLocation){
ViewModel.requestUserLocationForOnce()
}
.foregroundColor(.white)
.cornerRadius(8)
}
}
struct AreaMap: View {
@Binding var region: MKCoordinateRegion
var body: some View {
let binding = Binding(
get: { self.region },
set: { newValue in
DispatchQueue.main.async {
self.region = newValue
}
}
)
return Map(coordinateRegion: binding, showsUserLocation: true)
.ignoresSafeArea()
}
}
final class ContenViewModal: NSObject, ObservableObject, CLLocationManagerDelegate{
@Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 20, longitude: 90), span: MKCoordinateSpan(latitudeDelta: 100, longitudeDelta: 100))
let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
}
func requestUserLocationForOnce() {
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latestLocation = locations.first else {
//show error
return
}
self.region = MKCoordinateRegion(center: latestLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}
}
struct OverallView_Previews: PreviewProvider {
static var previews: some View {
OverallView()
}
}
Well, this is different from what you actually asked us for. You said: "I need my toolBar buttons to navigate to other SwiftUi views", which I showed you how to do. What you actually mean is you want to change the current view.
You could have your toolbar view showing on a MainView(), and the contents of the MainView() is changed depending on which button you've pressed, but the toolbar view remains on top. Something like:
struct MainView: View {
var body: some View {
ZStack {
ShowView(viewId: 1)
ToolbarView(selectedViewId: 1)
}
}
}
struct ShowView(): View {
var viewId: Int
var body: some View {
if(viewId == 1) {
// HomeView()
} else if(viewId == 2) {
...
}
}
}