Swiftui Navigation Question Using NavigationLink to specific Tab - swiftui

Basically I have the below listed code working with the exception of getting to view 4. I'm guessing I've messed up the binding in some way, I just can't figure out what. Can anyone help make this happen? Here's where I am with code:
import SwiftUI
struct ContentView: View {
#State var activeView: Int = 0
#State var activeNavigationLink: Int = 0
let items = ["View1", "View2", "View3", "View4"]
func navigationLinkBinding(id: Int) -> Binding<Bool> {
.init { () -> Bool in
activeNavigationLink == id
} set: { (newValue) in
if newValue {
activeNavigationLink = id
} else {
activeNavigationLink = 0
}
}
}
var body: some View {
TabView(selection: Binding<Int> (
get: {
activeView
}, set: {
activeView = $0
activeNavigationLink = 0
}))
{
NavigationView {
HStack(spacing: 40) {
VStack {
NavigationLink(
destination: NewView(), isActive: navigationLinkBinding(id: 2), label: {
Rectangle()
.fill(Color.red)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To View 3")
}
VStack{
NavigationLink(
destination: NewView(), isActive: navigationLinkBinding(id: 3), label: {
Rectangle()
.fill(Color.red)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To View 4")
}
}.navigationTitle("")
.navigationBarHidden(true)
}
.tabItem {
Image(systemName: "a.circle")
Text("Main")
}
.tag(0)
Text("View 5")
.padding()
.tabItem {
Image(systemName: "b.circle")
Text("View 3")
}
.tag(1)
}
}
}
The below is in the same file as the above. It is where view 4 resides:
struct NewView: View {
let items = ["View1", "View2", "View3", "View4"]
var body: some View {
HStack {
TabView {
VStack {
Rectangle()
.fill(Color.green)
.cornerRadius(12)
.frame(width: 70, height: 70)
Text(items[2])
}
.tabItem {Image(systemName: "a.circle"); Text("GREEN")}
.id(2)
VStack {
Rectangle()
.fill(Color.blue)
.cornerRadius(12)
.frame(width: 70, height: 70)
Text(items[3])
}
.tabItem {Image(systemName: "b.circle"); Text("BLUE")}
.id(3)
}
.navigationTitle("")
.navigationBarHidden(true)
.tabViewStyle(.page)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Here's the working code:
// ContentView.swift
// navtest
//
// Created by MRBarclay on 4/9/22.
//
import SwiftUI
struct ContentView: View {
#State var activeView: Int = 0
#State var activeNavigationLink: Int = 0
func navigationLinkBinding(id: Int) -> Binding<Bool> {
.init { () -> Bool in
activeNavigationLink == id
} set: { (newValue) in
if newValue {
activeNavigationLink = id
} else {
activeNavigationLink = 0
}
}
}
var body: some View {
TabView(selection: Binding<Int> (
get: {
activeView
}, set: {
activeView = $0
activeNavigationLink = 0
}))
{
VStack {
Text("Main 1")
Rectangle()
.fill(Color.yellow)
.cornerRadius(12)
.frame(width: 70, height: 70)
}
.tabItem {Image(systemName: "1.circle"); Text("Main 1")}
.tag(1)
NavigationView {
HStack(spacing: 40) {
VStack {
Text("Main 2")
NavigationLink(
destination: NewView(selectedItem: 1), isActive: navigationLinkBinding(id: 1), label: {
Rectangle()
.fill(Color.red)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 1")
}
VStack{
Text("Main 2")
NavigationLink(
destination: NewView(selectedItem: 2), isActive: navigationLinkBinding(id: 2), label: {
Rectangle()
.fill(Color.red)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 2")
}
VStack{
Text("Main 2")
NavigationLink(
destination: NewView(selectedItem: 3), isActive: navigationLinkBinding(id: 3), label: {
Rectangle()
.fill(Color.red)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 3")
}
}.navigationTitle("")
.navigationBarHidden(true)
}
.tabItem {Image(systemName: "2.circle"); Text("Main 2")}
.tag(2)
NavigationView {
HStack(spacing: 40) {
VStack {
Text("Main 3")
NavigationLink(
destination: NewView(selectedItem: 1), isActive: navigationLinkBinding(id: 1), label: {
Rectangle()
.fill(Color.blue)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 1")
}
VStack{
Text("Main 3")
NavigationLink(
destination: NewView(selectedItem: 2), isActive: navigationLinkBinding(id: 2), label: {
Rectangle()
.fill(Color.blue)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 2")
}
VStack{
Text("Main 3")
NavigationLink(
destination: NewView(selectedItem: 3), isActive: navigationLinkBinding(id: 3), label: {
Rectangle()
.fill(Color.blue)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 3")
}
}.navigationTitle("")
.navigationBarHidden(true)
}
.tabItem {Image(systemName: "3.circle"); Text("Main 3")}
.tag(3)
NavigationView {
HStack(spacing: 40) {
VStack {
Text("Main 4")
NavigationLink(
destination: NewView(selectedItem: 1), isActive: navigationLinkBinding(id: 1), label: {
Rectangle()
.fill(Color.green)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 1")
}
VStack{
Text("Main 4")
NavigationLink(
destination: NewView(selectedItem: 2), isActive: navigationLinkBinding(id: 2), label: {
Rectangle()
.fill(Color.green)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 2")
}
VStack{
Text("Main 4")
NavigationLink(
destination: NewView(selectedItem: 3), isActive: navigationLinkBinding(id: 3), label: {
Rectangle()
.fill(Color.green)
.cornerRadius(12)
.frame(width: 70, height: 70)
}).isDetailLink(false)
Text("To Sub 3")
}
}.navigationTitle("")
.navigationBarHidden(true)
}
.tabItem {Image(systemName: "4.circle"); Text("Main 4")}
.tag(4)
}
}
}
struct NewView: View {
#State var selectedItem: Int
#State var activeNavigationLink: Int = 0
var body: some View {
TabView(selection: $selectedItem) {
VStack {
Rectangle()
.fill(Color.blue)
.cornerRadius(12)
.frame(width: 70, height: 70)
Text("One")
}
.tabItem {Image(systemName: "a.circle"); Text("BLUE")}
.tag(1)
VStack {
Rectangle()
.fill(Color.green)
.cornerRadius(12)
.frame(width: 70, height: 70)
Text("Two")
}
.tabItem {Image(systemName: "b.circle"); Text("GREEN")}
.tag(2)
VStack {
Rectangle()
.fill(Color.orange)
.cornerRadius(12)
.frame(width: 70, height: 70)
Text("Three")
}
.tabItem {Image(systemName: "b.circle"); Text("ORANGE")}
.tag(3)
}
.navigationTitle("")
.navigationBarHidden(true)
.tabViewStyle(.page)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Related

NavigationLink to NavigationStack or NavigationSplitView iOS 16

import SwiftUI
import MapKit
struct SearchView: View {
#StateObject var locationManager: LocationManager = .init()
#State var navigationTag: String?
var body: some View {
VStack{
HStack(spacing: 15){
Button{
}label: {
Image(systemName: "chevron.left")
.font(.title3)
.foregroundColor(Color("New Pink"))
}
Text("Search your place")
.font(.title3)
.fontWeight(.semibold)
}
.frame(maxWidth: .infinity,alignment: .leading)
HStack(spacing: 10){
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
TextField("type your places",text: $locationManager.searchText)
}
.padding(.vertical, 12)
.padding(.horizontal)
.background{
RoundedRectangle(cornerRadius: 10, style: .continuous)
.strokeBorder(.gray)
}
.padding(.vertical,10)
if let places = locationManager.fetchedPlaces,!places.isEmpty{
List{
ForEach(places,id: \.self){place in
Button{
if let coordinate = locationManager.userLocation?.coordinate{
locationManager.pickedLocation = .init(latitude: coordinate.latitude, longitude: coordinate.longitude)
locationManager.mapView.region = .init(center: coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
locationManager.addDraggablePin(coordinate: coordinate)
locationManager.updatePlacemark(location: .init(latitude: coordinate.latitude, longitude: coordinate.longitude))
}
navigationTag = "MAPVIEW"
} label:{
HStack(spacing: 15){
Image(systemName: "mappin.circle.fill")
.font(.title2)
.foregroundColor(.gray)
VStack(alignment: .leading, spacing: 6){
Text(place.name ?? "")
.font(.title3.bold())
.foregroundColor(.primary)
Text(place.locality ?? "")
.font(.caption)
.foregroundColor(.gray)
}
}
}
}
}
.listStyle(.plain)
}else{
Button{
if let coordinate = locationManager.userLocation?.coordinate{
locationManager.mapView.region = .init(center: coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
locationManager.addDraggablePin(coordinate: coordinate)
locationManager.updatePlacemark(location: .init(latitude: coordinate.latitude, longitude: coordinate.longitude))
navigationTag = "MAPVIEW"
}
} label:{
Label{
Text("Use Current Location")
.font(.callout)
} icon: {
Image(systemName: "location.north.circle.fill")
}
.foregroundColor(.green)
}
.frame(maxWidth: .infinity,alignment: .leading)
}
}
.padding()
.frame(maxHeight: .infinity,alignment: .top)
.background{
NavigationLink(tag: "MAPVIEW", selection: $navigationTag){
MapViewSelection()
.environmentObject(locationManager)
.navigationBarHidden(true)
} label: {}
.labelsHidden()
}
}
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
SearchView()
}
}
struct MapViewSelection: View{
#EnvironmentObject var locationManager: LocationManager
#Environment(\.dismiss) var dismiss
var body: some View{
ZStack{
MapViewHelper()
.environmentObject(locationManager)
.ignoresSafeArea()
Button {
dismiss()
} label: {
Image(systemName: "chevron.left")
.font(.title2.bold())
.foregroundColor(.primary)
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
if let place = locationManager.pickedPlaceMark{
VStack(spacing: 15){
Text("Confirm Location")
.font(.title2.bold())
HStack(spacing: 15){
Image(systemName: "mappin.circle.fill")
.font(.title2)
.foregroundColor(.gray)
VStack(alignment: .leading, spacing: 6){
Text(place.name ?? "")
.font(.title3.bold())
Text(place.locality ?? "")
.font(.caption)
.foregroundColor(.gray)
}
}
.frame(maxWidth: .infinity,alignment: .leading)
.padding(.vertical,10)
Button{
} label: {
Text("Confirm Location")
.fontWeight(.semibold)
.frame(maxWidth: .infinity)
.padding(.vertical,12)
.background{
RoundedRectangle(cornerRadius: 10, style: .continuous)
.fill(.green)
}
.overlay(alignment: .trailing){
Image(systemName: "arrow.right")
.font(.title3.bold())
.padding(.trailing)
}.foregroundColor(.white)
}
}.padding()
.background{
RoundedRectangle(cornerRadius: 10, style: .continuous)
.fill(.white)
.ignoresSafeArea()
}
.frame(maxHeight: .infinity, alignment: .bottom)
}
}.onDisappear{
locationManager.pickedLocation = nil
locationManager.pickedPlaceMark = nil
locationManager.mapView.removeAnnotations(locationManager.mapView.annotations)
}
}
}
struct MapViewHelper: UIViewRepresentable{
#EnvironmentObject var locationManager: LocationManager
func makeUIView(context: Context) -> MKMapView {
return locationManager.mapView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
}
I was trying to clone the swiftUI MapKit app
however there is an issue that In iOS 16.0 NavigationLink has been deprecated (SearchView - line 108ish) with a such warning as 'init(tag:selection:destination:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a List within a NavigationStack or NavigationSplitView
so how can i migrate the 'init(tag:selection:destination:label:)' to NavigationLink(value:label:) in this case
https://www.youtube.com/watch?v=fjZd1CwjlxQ&list=LL&index=3&t=1s check this video from 09:17 to 11:11

image picker in swift showing abnormal behaviour while selecting an image from gallery

when i pick an image using Image Picker from my Quick add View it kills all previous views and take me to the HomePage Tab which is Meal Planner View.Sometimes it works normal only on IOS device. It never works fine on simulator as i tried different simulators in Xcode. It also works fine in simulator and IOS device when i remove my TabBar and run directly from Meal Planner View.
kindly suggest something helpful.
import SwiftUI
struct QuickAddView: View {
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var viewName: String
#ObservedObject var vm = MealViewModel()
#State private var showPicker = false
#State private var selectedImage: UIImage?
#State var mealTitle: String = ""
#State var text: String = ""
// #State var descrption: String = ""
var body: some View {
VStack{
navBarView()
ScrollView(showsIndicators: false){
VStack {
VStack(alignment: .leading) {
Button {
self.showPicker.toggle()
} label: {
if selectedImage == nil {
ZStack {
Image("qorma")
.resizable()
.frame( height: 100)
HStack{
Text("+").font(.custom(Nunito.Bold.rawValue, size: 22))
.foregroundColor(.white)
Text("Attach a Photo").font(.custom(Nunito.Regular.rawValue, size: 20))
.foregroundColor(.white)
}
}
}
else {
ZStack {
Image(uiImage: selectedImage!)
.resizable()
.frame( height: 100)
HStack{
Text("+").font(.custom(Nunito.Bold.rawValue, size: 22))
.foregroundColor(.white)
Text("Attach a Photo").font(.custom(Nunito.Regular.rawValue, size: 20))
.foregroundColor(.white)
}
}
}
}
}
CustomTextField(text: $vm.title, placeHolder: "Name")
.background(.white)
.padding([.vertical],30)
fields()
TextEditor(text: $vm.description)
.textFieldStyle(.roundedBorder)
.colorMultiply(.white)
.frame( height: 199)
.border(.gray, width: 0.5)
.cornerRadius(10)
.shadow(radius: 1)
.foregroundColor(.gray)
.padding([.top],30)
addButton()
}
}
}.padding()
.sheet(isPresented: $showPicker) {
ImagePicker(image: $selectedImage)
}
}
}
extension QuickAddView {
func nutrientFields(nutrientTitle: String,amount: Binding<String>) -> some View {
VStack(alignment: .leading){
HStack(){
Text(nutrientTitle)
.font(.custom(Nunito.Semibold.rawValue, size: 24))
.foregroundColor(.gray)
Spacer()
HStack{
ZStack{
TextField("0", text: amount)
.font(Font.system(size: 30))
.background(Color.white)
.foregroundColor(Color(ColorName.appAqua.rawValue))
.frame(width: 70)
}
.background(
Rectangle()
.foregroundColor(Color.white)
.opacity(0.2)
)
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color(ColorName.appAqua.rawValue), lineWidth: 2.5)
)
Text("(g)")
.font(.custom(Nunito.Semibold.rawValue, size: 30))
.foregroundColor(Color(ColorName.appAqua.rawValue))
}.padding([.horizontal])
// .font(.custom(Nunito.Semibold.rawValue, size: 20))
// .foregroundColor(Color(ColorName.appAqua.rawValue))
// .frame(width: 40, height: 30)
}
}
}
func calorieField (nutrientTitle: String,amount: Binding<String>) -> some View {
VStack(alignment: .leading){
HStack(){
Text(nutrientTitle)
.font(.custom(Nunito.Semibold.rawValue, size: 24))
.foregroundColor(.gray)
Spacer()
HStack{
ZStack{
TextField("0", text: amount)
.font(Font.system(size: 30))
.background(Color.white)
.foregroundColor(Color(ColorName.appAqua.rawValue))
.frame(width: 70)
}
.background(
Rectangle()
.foregroundColor(Color.white)
.opacity(0.2)
)
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color(ColorName.appAqua.rawValue), lineWidth: 2.5)
)
}.padding([.horizontal],59)
}
}
}
func fields() -> some View {
VStack(spacing: 20){
calorieField(nutrientTitle: "Calories",amount: $vm.cals)
Rectangle()
.fill(.gray)
.frame( height: 1)
nutrientFields(nutrientTitle: "Carbohydrates",amount: $vm.carb)
Rectangle()
.fill(.gray)
.frame( height: 1)
nutrientFields(nutrientTitle: "Proteins",amount: $vm.aminos)
Rectangle()
.fill(.gray)
.frame( height: 1)
nutrientFields(nutrientTitle: "Fats",amount: $vm.lipids)
}
}
func navBarView() -> some View {
ZStack {
HStack{
Button ( action: { self.presentationMode.wrappedValue.dismiss() },
label: {
Image(systemName: "arrow.backward").frame(width: 16, height: 16)
.foregroundColor(Color(ColorName.appAqua.rawValue))
})
Spacer()
}
Text("Quick Add")
.font(.custom(Nunito.Bold.rawValue, size: 22.5))
.foregroundColor(Color(ColorName.appAqua.rawValue))
}
}
func addButton() -> some View {
// NavigationLink(
// destination: BreakFastMainView(viewName: viewName, firebaseId: UserDefaults.standard.value(forKey: "FireBaseId") as! String) .navigationBarTitle("")
// .navigationBarHidden(true)
// ,
Button {
vm.addMealData(category: viewName, image: selectedImage ?? UIImage(), name: vm.title, cals: vm.cals, carbs: vm.carb, protein: vm.aminos, fat: vm.lipids, description: vm.description, firebaseId: UserDefaults.standard.value(forKey: "FireBaseId") as? String ?? "12345", action: {
self.presentationMode.wrappedValue.dismiss()
})
}
label: {
ZStack {
RoundedRectangle(cornerRadius: 10)
.frame(height: 50)
.foregroundColor(Color("btnBlue"))
Text("Add")
.font(.custom("Nunito-Bold", size: 20))
.foregroundColor(.white)
}.padding([.top], 32)
}
}
func data(_ name: String, _ amount: String) -> some View {
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 5)
.stroke( .gray, lineWidth: 0.5)
.frame(height: 56)
HStack {
Text(name)
.foregroundColor( .gray)
.font(.custom(Nunito.Semibold.rawValue, size: 20))
Spacer()
Text(amount)
.foregroundColor( Color(ColorName.appAqua.rawValue))
.font(.custom(Nunito.Semibold.rawValue, size: 20))
Text("g")
.foregroundColor( Color(ColorName.appAqua.rawValue))
.font(.custom(Nunito.Semibold.rawValue, size: 20))
}.padding()
}
}
}
struct QuickAddView_Previews: PreviewProvider {
static var previews: some View {
QuickAddView( viewName: "dfd")
}
}

Responsive design swiftUI

How can I make sure that my UI looks good on different devices such as the iPhone 13 and the iPhone 8?
Here is my code that is unresponsive
It is just a simple layout
import SwiftUI
let grey = Color(red: 0.928, green: 0.928, blue: 0.928)
let i = 0
var columns: [GridItem] =
Array(repeating: .init(.flexible()), count: 3)
let categories: [Category] = [
.init(name: "Pasta", imageName: "square.and.arrow.up"),
.init(name: "Asian", imageName: "square.and.arrow.down.fill"),
.init(name: "Main", imageName: "rectangle.portrait.and.arrow.right"),
.init(name: "Quick", imageName: "pencil.slash"),
.init(name: "Veg", imageName: "scribble.variable"),
.init(name: "Dessert", imageName: "pencil.circle.fill"),
]
struct SearchView: View {
var body: some View {
VStack{
Text("Search For Recipes")
.font(.system(size: 35))
.padding(.top, 80)
.padding(.horizontal, -70)
.frame(width: 200, height: 10, alignment: .leading)
Spacer()
SearchBox()
.padding(.top, 50)
Spacer()
Recomend()
.padding(.top)
.padding(.leading, -100)
Spacer()
LazyVGrid(columns: columns){
ForEach((0...5), id: \.self) { _ in
MenuChoice()
}
}
.padding(20)
.padding(.bottom, 100)
.padding(.top)
.frame(maxWidth: .infinity)
.frame(height:500)
}
}
}
struct SearchBox: View {
var body: some View{
VStack{
HStack{
Image(systemName: "magnifyingglass")
Text("Search for recipes")
}
.frame(width: 200, height: 10, alignment: .leading)
.padding(.trailing, 60)
}
.frame(width: 300, height: 50)
.background(grey)
.cornerRadius(8)
.padding(.trailing, 50)
}
}
struct MenuChoice: View{
var body: some View{
ZStack{
ForEach(categories, id: \.self) { category in
VStack{
Spacer()
.frame(width: 100, height: 100)
.background(Color(red: 50, green: 207, blue: 255))
.cornerRadius(5)
.shadow(color: .init(.sRGB, white: 0.7, opacity: 1), radius: 4, x: 0.0, y: 2)
.padding(.bottom)
.overlay(
VStack(spacing: 8) {
Image(systemName: category.imageName)
.padding(.top, -10)
.font(.system(size: 30))
Text(category.name)
.foregroundColor(.black)
.font(.system(size: 20))
.padding(.top, 5)
}
)
}
}
}
}
}
struct Recomend: View{
var body: some View{
Button {
print("Image tapped!")
} label: {
ZStack{
RoundedRectangle(cornerRadius: 8)
.frame(width: 250, height: 50)
.foregroundColor(grey)
Text("See All Recomended Recipes")
.foregroundColor(Color.black)
}
}
}
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
SearchView()
.previewDevice(PreviewDevice(rawValue: "iPhone 13"))
SearchView()
.previewDevice(PreviewDevice(rawValue: "iPhone 8"))
}
}
On the iPhone 13 it looks good but on the iPhone 8 it looks squashed and not good.
How can i make my code responsive for different screen heights/widths?
Following the comments you could go in this direction:
This adapts nicely and also is a lot less code.
struct ContentView: View {
var body: some View {
VStack{
Text("Search For Recipes")
.font(.system(size: 35))
.frame(maxWidth: .infinity, alignment: .leading)
SearchBox()
.padding(.bottom)
.padding(.bottom)
Recomend()
Spacer()
LazyVGrid(columns: columns, spacing: 18){
ForEach(categories) { category in
MenuChoice(category: category)
}
}
Spacer()
}
.padding()
}
}
struct SearchBox: View {
var body: some View{
VStack {
HStack{
Image(systemName: "magnifyingglass")
Text("Search for recipes")
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(grey)
.cornerRadius(8)
}
}
struct MenuChoice: View{
let category: Category
var body: some View{
VStack(spacing: 8) {
Image(systemName: category.imageName)
.padding(.top)
.font(.system(size: 30))
Text(category.name)
.foregroundColor(.black)
.font(.system(size: 20))
.padding(.bottom)
}
.frame(width: 100, height: 100)
.background(Color(red: 50, green: 207, blue: 255))
.cornerRadius(5)
.shadow(color: .init(.sRGB, white: 0.7, opacity: 1), radius: 4, x: 0.0, y: 2)
}
}
struct Recomend: View{
var body: some View{
Button {
print("Image tapped!")
} label: {
Text("See All Recomended Recipes")
.padding()
.foregroundColor(Color.black)
.frame(maxWidth: .infinity, alignment: .leading)
.background(grey)
.cornerRadius(8)
}
}
}

cannot use NavigationLink?

NavigationView {
ScrollView{
.navigationTitle("Home")
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
VStack{
HStack{
NavigationLink(
destination: Home(),
label: {
Image(systemName: "house")
.resizable()
.frame(width: 25.0, height: 20.0)
.foregroundColor(.blue)
.padding()
})
}
}
I cant use NavigationLink(destination : Text("Go home page"),label:{}
It is menu bar I want to freeze this menu bar when I scroll down or up
Thank you so much
import SwiftUI
struct HomeNav: View {
#State var isActiveHome: Bool = false
var body: some View {
NavigationView {
ScrollView{
Text("Main")
NavigationLink(
destination: Text("Home").navigationTitle("Home"),
isActive: $isActiveHome,
label: {
Image(systemName: "house")
.resizable()
.frame(width: 25.0, height: 20.0)
.foregroundColor(.blue)
.padding()
}).hidden().frame(width: 0, height: 0, alignment: .center)
.navigationTitle("Main")
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
VStack{
HStack{
Button(action: {
isActiveHome = true
}, label: {
Image(systemName: "house")
.resizable()
.frame(width: 25.0, height: 20.0)
.foregroundColor(.blue)
.padding()
})
}
}
}
}
}
}
}
}

Navigation Link leading to gray screen in Swift UI

I am new to SwiftUI and am having a bug where my entire screen turns gray when I use too many Navigation Links.
I cannot find any solutions while researching the bug.
I am running the project on the newest version of Xcode 12.4.
My current setup is to have 2 different swiftUI Views, each containing a Navigation Link to the other.
This is what it looks like
Code:
PageOne.swift
struct PageOne: View {
var body: some View {
NavigationView {
VStack {
Text("This is page 1")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageTwo(),
label: {
VStack {
Text("Go to Page 2")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
PageTwo.swift
struct PageTwo: View {
var body: some View {
NavigationView {
VStack {
Text("This is page 2")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageOne(),
label: {
VStack {
Text("Go to Page 1")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
Project file
You should only have one NavigationView in the view hierarchy.
Try creating one NavigationView at the root level:
struct ContentView: View {
var body: some View {
NavigationView {
PageOne()
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
and then remove NavigationView from subviews:
struct PageOne: View {
var body: some View {
VStack {
Text("This is page 1")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageTwo(),
label: {
VStack {
Text("Go to Page 2")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
struct PageTwo: View {
var body: some View {
VStack {
Text("This is page 2")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageOne(),
label: {
VStack {
Text("Go to Page 1")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}