简体   繁体   English

如何使用 SwiftUI 消除嵌套 NavigationView 中的空间

[英]How to get rid of space in nested NavigationView with SwiftUI

Im currently working on a project for iOS using SwiftUI.我目前正在使用 SwiftUI 为 iOS 开展一个项目。 I have 3 pages (MainMenu, CalendarList, and DateDetails.)我有 3 页(MainMenu、CalendarList 和 DateDetails。)

On the 2nd page (CalenderList) there is an empty space between the top of the screen and the actual NavigationBarTitle.在第二页 (CalenderList) 上,屏幕顶部和实际 NavigationBarTitle 之间有一个空白区域。

第 2 页的图像 - CalendarList

on the third page, you can see the back button (to the MainMenu) and there is two empty spaces at the top.在第三页上,您可以看到返回按钮(到 MainMenu) 顶部有两个空白区域。

第 3 页的图片 - 日期详情

I've seen people use.navigationBarHidden to fix this, but i haven't been able to implement it in a way that fixes the problem.我见过人们使用.navigationBarHidden 来解决这个问题,但我无法以解决问题的方式实现它。

Am i using NavigationView() incorrectly?我是否错误地使用 NavigationView()? or is there a special trick?还是有什么特别的技巧?

Here is the code for the MainMenu:这是 MainMenu 的代码:

import SwiftUI

struct MainMenu: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Calendar")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                    .foregroundColor(Color(red: 0.055, green: 0.173, blue: 0.322))
                    .padding(.top, 55.0)
                Text("Main Menu")
                    .font(.headline)
                    .foregroundColor(Color(red: 0.635, green: 0.635, blue: 0.635, opacity: 1.0))
                
                /*Image("Logo")
                    .resizable()
                    .frame(width: 150.0, height: 150.0)*/

                Spacer()
                
                HStack {
                    NavigationLink(destination: CalendarList()) {
                        Image(systemName: "calendar")
                            .resizable()
                            .frame(width: 75.0, height: 75.0)
                            .padding()
                            
                            
                    }
                    
                    NavigationLink(destination: CalendarList()) {
                        Image(systemName: "gear")
                            .resizable()
                            .frame(width: 75.0, height: 75.0)
                            .padding()
                    }
                    
                }
                
                
                
                HStack {
                    NavigationLink(destination: StudentInfo()) {
                        Image(systemName: "info.circle")
                            .resizable()
                            .frame(width: 75.0, height: 75.0)
                            .padding()
                    }
                    
                    NavigationLink(destination: CalendarList()) {
                        Image(systemName: "exclamationmark.circle")
                            .resizable()
                            .frame(width: 75.0, height: 75.0)
                            .padding()
                    }
                }
                
                Spacer()
            }
        }
        
    }
}

Here is the code for CalendarList (page 2):这是 CalendarList 的代码(第 2 页):

import SwiftUI

struct CalendarList: View {
    var body: some View {
        NavigationView {
            List(calendarData, id: \.date) { Calendar in
                
                if Calendar.collab {
                    NavigationLink(destination: DateDetails(calendar: Calendar)) {
                        CalendarRow(calendar: Calendar)
                    }
                } else {
                CalendarRow(calendar: Calendar)
                }
            }
            .navigationBarTitle(Text("Schedule"))
        }
        
    }
}

And here is the code for DateDetails (page 3):这是 DateDetails 的代码(第 3 页):

import SwiftUI

struct DateDetails: View {
    var calendar: Calendar
    
    var body: some View {
        NavigationView {
            VStack (alignment: .center) {
                //Image("Logo")
                
                    
                HStack {
                    Text(calendar.month.prefix(4) + ".")
                        .font(.largeTitle)
                    Text(String(calendar.date).suffix(1))
                        .font(.largeTitle)
                    
                    Spacer()
                }
                
                HStack {
                    Text(calendar.schedule)
                        .font(.title)
                    Spacer()
                }
                
                Spacer()
                    .frame(height: 30.0)
                
                Text(calendar.info)
                    .font(.body)
                
                Spacer()
            }
            .navigationBarTitle(String(calendar.date).prefix(4).suffix(2) + "/" + String(calendar.date).suffix(2))
                
            .padding()
        }
        
    }
}

Only use NavigationView at the top level, you don't need to add it in every subscreen, just remove it from CalendarList and DateDetails and it will fix your spacing issue仅在顶层使用 NavigationView,您无需在每个子屏幕中添加它,只需将其从 CalendarList 和 DateDetails 中删除即可解决您的间距问题

I think you can delete the NavigationView of DateDetails.我认为您可以删除 DateDetails 的 NavigationView。

If you want to change the navigationbar, you may want to edit navigationBarItems or change navigationBarHidden to true and customize it.如果要更改导航栏,您可能需要编辑 navigationBarItems 或将 navigationBarHidden 更改为 true 并对其进行自定义。

https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-bar-items-to-a-navigation-view https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-bar-items-to-a-navigation-view

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM