I have successfully make an image tapable to direct it to other view by using Navigation Link and View. But there has the space around the Navigation Bar that I was unable to remove.
I have three different views. SwiftUI View which contain the image, List View and Detail View.
I need the Navigation Title on List and Detail View but it has double space on the Navigation area when tap from the image.
SwiftUI View Code
struct SwiftUIView: View {
var body: some View {
NavigationView {
ZStack {
ScrollView {
ZStack {
NavigationLink(destination: RecipeListView()) {
VStack(spacing: 40){
Spacer()
Image("dessert")
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: 200, alignment: .bottom)
.cornerRadius(20)
.shadow(color: .gray, radius: 10, x: 0, y: 5)
.overlay(VStack {
Spacer()
Text("Yo")
.padding(.bottom, 20)
.opacity(0.85)
.font(.system(size:30, weight: .black))
.foregroundColor(.white)
})
}.padding(.horizontal, 40)
}
}
.edgesIgnoringSafeArea(.vertical)
}
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}
List View Code
struct RecipeListView: View {
// Reference the view model
@ObservedObject var model = RecipeModel()
var body: some View {
NavigationView {
List(model.recipes) { r in
NavigationLink(
destination: RecipeDetailView(recipe:r),
label: {
// MARK: Row item
HStack(spacing: 20.0) {
Image(r.image)
.resizable()
.scaledToFill()
.frame(width: 50, height: 50, alignment: .center)
.clipped()
.cornerRadius(5)
Text(r.name)
}
})
}
.navigationBarTitle("All Recipes",displayMode: .inline)
}
}
}
struct RecipeListView_Previews: PreviewProvider {
static var previews: some View {
RecipeListView()
}
}
Detail View Code
struct RecipeDetailView: View {
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var recipe:Recipe
var body: some View {
ScrollView {
VStack (alignment: .leading) {
// MARK: Recipe Image
Image(recipe.image)
.resizable()
.scaledToFill()
VStack {
Text("Servings:" + " " + recipe.servings)
.font(.headline)
.padding([.bottom, .top], 5)
}
.padding(.horizontal)
// MARK: Ingredients
VStack(alignment: .leading) {
Text("Ingredients")
.font(.headline)
.padding([.bottom, .top], 5)
ForEach (recipe.ingredients, id:\.self) { item in
Text("• " + item)
}
}
.padding(.horizontal)
// MARK: Divider
Divider()
// MARK: Directions
VStack(alignment: .leading) {
Text("Directions")
.font(.headline)
.padding([.bottom, .top], 5)
ForEach(0..<recipe.directions.count, id: \.self) { index in
Text(String(index+1) + ". " + recipe.directions[index])
.padding(.bottom, 5)
}
}
.padding(.horizontal)
}
}
.navigationBarTitle(recipe.name)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action : {
self.mode.wrappedValue.dismiss()
}){
Image(systemName: "arrow.left")
.foregroundColor(.black)
})
}
}
struct RecipeDetailView_Previews: PreviewProvider {
static var previews: some View {
// Create a dummy recipe and pass it into the detail view so that we can see a preview
let model = RecipeModel()
RecipeDetailView(recipe: model.recipes[0])
}
}
I had a custom NavigationBarButton on Detail View but it seem like when I tap on the image and move to List View, it will show the default Navigation Bar from List View and same issue when move from List View to Detail View. Is there anyway to show only one Navigation Bar on three Views?
Thank you.