Removing large amounts of whitespace in a SwiftUI subview

你离开我真会死。 提交于 2021-02-11 18:15:52

问题


Demonstration of whitespace problem

When I nest a NavigationView within a NavigationView, an enormous amount of whitespace separates the back button and the new navigation bar title. Is there something I'm doing wrong in terms of setting up my SwiftUI views?

import SwiftUI

struct Dashboard: View {
    @EnvironmentObject var user: User
    let courses = Course.exampleCourses()

    var body: some View {
        NavigationView {
            List(courses) { course in
                NavigationLink(destination: CourseView(course: course)) {
                    Text(course.name)
                }
            }.navigationBarTitle("Welcome, \(user.first)!")
        }
    }
}
import SwiftUI

struct CourseView: View {
    // @ObservedObject allows us to update views whenever values in course change
    @ObservedObject var course: Course
    @EnvironmentObject var user: User

    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: WritingPromptView(prompt: "What is your course goal, \(user.first)?", explanationText: "This is the answer", textLocation: self.$course.goal)) {
                    Text("Course Goal")
                }
                NavigationLink(destination: NotepadView(parent: self.course)) {
                    Text("Notepad")
                }
                NavigationLink(destination: WritingPromptView(prompt: "<Reflection prompt goes here>", explanationText: "<How to reflect goes here>", textLocation: self.$course.reflection)) {
                    Text("Reflection")
                }

            }.navigationBarTitle(course.name)
        }
    }
}

回答1:


It's a double NavigationBar. Just remove NavigationView from your CourseView. If you have Previews for CourseView, you will probably want to wrap it NavigationView there.



来源:https://stackoverflow.com/questions/58493091/removing-large-amounts-of-whitespace-in-a-swiftui-subview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!