I have obviously missed some subtle bit of code but I can't see what I am missing. I am getting a warning about my NavigationLink.
Result of 'NavigationLink<Label, Destination>' initializer is unused
Below I have included the code that has caused the warning and the code of the target of the Navigation link. The link does not work, though the button does, as proved by the print statement (see below). Here is the code that caused the warning:
import SwiftUI
import SwiftData
struct Main_Menu_iPad: View {
@Environment(\.modelContext) private var context
@Query private var readings: [Readings]
var body: some View {
ZStack {
Image("iPad Background 810 X 1060")
.resizable()
.scaledToFill()
.ignoresSafeArea()
VStack {
HStack{
Image("Diabetes Control Logo 1024X1024")
.resizable()
.frame(width: 100, height: 100, alignment: .leading)
.padding(.leading, 40)
Text("Diabetes Control")
.font(Font.custom("SnellRoundhand-Black", size: 40))
.background(Color(red: 255, green: 253, blue: 208))
.shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3)
.padding(.leading, 150)
Spacer()
}.padding(.bottom, 35)
HStack {
Text("What would you like to do : ")
.font(Font.custom("SnellRoundhand-Black", size: 30))
.background(Color(red: 128, green: 128, blue: 128))
.shadow(color: Color(red: 126, green: 128, blue: 128), radius: 3)
Spacer()
}.padding(.leading, 35)
.padding(.bottom,35)
NavigationStack {
HStack {
Button {
print("I have been pressed")
NavigationLink {
iPadReadingsEntry()
} label: {
Text("Enter a BLOOD Glucose reading")
}
} label: {
Text("Enter a Blood Glucose Reading")
}.background(Color(red: 255, green: 253, blue: 208))
.foregroundColor(.black)
.font(Font.custom("SnellRoundhand", size: 24))
Spacer()
}.padding(.leading, 60)
.padding(.bottom, 25)
Spacer()
}.background(.blue)
Spacer()
}
}
}
}
#Preview {
Main_Menu_iPad()
}
And this is the code of the target view:
import SwiftUI
struct iPadReadingsEntry: View {
var body: some View {
ZStack {
Image("iPad Background 810 X 1060")
.resizable()
.scaledToFill()
.ignoresSafeArea()
VStack {
HStack{
Image("Diabetes Control Logo 1024X1024")
.resizable()
.frame(width: 100, height: 100, alignment: .leading)
.padding(.leading, 40)
Text("Diabetes Control")
.font(Font.custom("SnellRoundhand-Black", size: 40))
.background(Color(red: 255, green: 253, blue: 208))
.shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3)
.padding(.leading, 150)
Spacer()
}.padding(.bottom, 35)
Spacer()
}
}
}
}
#Preview {
iPadReadingsEntry()
}
Finally is there a way of switching View without using a NavigationStack/NamigastinLink as it covers my background image?
The issue is you are placing the NavigationLink
inside of a button's action closure.
The first argument of the Button
is action
of type () -> Void
(no parameters, no return). You are placing a NavigationLink
inside which isn't being used at all, causing the warning to be generated.
To fix this you need to use either the Button
or the NavigationLink
.
As for your second problem, you are placing the NavigationStack
inside of a VStack
so the other content won't "show properly". Instead, try moving the NavigationStack
to the top level.
This code should solve both issues:
NavigationStack { // move to top
ZStack {
Image("iPad Background 810 X 1060")
.resizable()
.scaledToFill()
.ignoresSafeArea()
VStack {
HStack{
Image("Diabetes Control Logo 1024X1024")
.resizable()
.frame(width: 100, height: 100, alignment: .leading)
.padding(.leading, 40)
Text("Diabetes Control")
.font(Font.custom("SnellRoundhand-Black", size: 40))
.background(Color(red: 255, green: 253, blue: 208))
.shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3)
.padding(.leading, 150)
Spacer()
}.padding(.bottom, 35)
HStack {
Text("What would you like to do : ")
.font(Font.custom("SnellRoundhand-Black", size: 30))
.background(Color(red: 128, green: 128, blue: 128))
.shadow(color: Color(red: 126, green: 128, blue: 128), radius: 3)
Spacer()
}.padding(.leading, 35)
.padding(.bottom,35)
HStack {
NavigationLink { // change to use only navlink
iPadReadingsEntry()
} label: {
Text("Enter a BLOOD Glucose reading")
}.background(Color(red: 255, green: 253, blue: 208))
.foregroundColor(.black)
.font(Font.custom("SnellRoundhand", size: 24))
Spacer()
}.padding(.leading, 60)
.padding(.bottom, 25)
Spacer()
}.background(.blue)
Spacer()
}
}