简体   繁体   English

在 SwiftUI 的 NavigationView 中隐藏顶部空间

[英]Hide top space in SwiftUI's NavigationView

I'm having trouble to remove the top space inside a NavigationView .我无法删除NavigationView内的顶部空间。

在此处输入图像描述 在此处输入图像描述

You can see that in the first screen, I have the "Job filters" title, which is later shown in the back button when for example tapping on the Company picker, but the space that the title used to occupy is now empty.您可以看到,在第一个屏幕中,我有“工作筛选器”标题,例如在Company选择器上点击时,该标题稍后会显示在后退按钮中,但该标题过去占据的空间现在是空的。 How can this be removed?如何删除?

The body's var of the view is:主体的视图变量是:

var body: some View {
        NavigationView {
            Form {
                ...
                ...
                Picker("FilterView.Company".localized, selection: $draft.company) {
                    SearchBar(searchText: $searchText)
                    Text("FilterView.Company.AllCompanies".localized).tag(nil as Company?)
                    if searchText.isEmpty {
                        ForEach(companiesFetchedResults) {
                            Text($0.companyName).tag($0 as Company?)
                        }
                    } else {
                        ForEach(companiesFetchedResults.filter {
                            $0.companyName.contains(searchText)
                        }) {
                            Text($0.companyName).tag($0 as Company?)
                        }
                    }
                }
            }
            .navigationBarTitle("FilterViewTitle".localized)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { cancel }
                ToolbarItem(placement: .navigationBarTrailing) { done }
            }
        }
    }

Edit编辑

As suggested by @Stefan I've managed to extract the Form into a separate view, but it still doesn't work:正如@Stefan 所建议的,我已经设法将Form提取到一个单独的视图中,但它仍然不起作用:

FilterView : FilterView

var body: some View {
        NavigationView {
            FormView(draft: $draft)
                .navigationBarTitle("FilterViewTitle".localized)
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) { cancel }
                    ToolbarItem(placement: .navigationBarTrailing) { done }
                }
        }
    }

FormView : FormView

var body: some View {
        VStack {
            Form {
                ...
                ...
                Picker("FilterView.Company".localized, selection: draft.company) {
                    SearchBar(searchText: $searchText)
                    Text("FilterView.Company.AllCompanies".localized).tag(nil as Company?)
                    if searchText.isEmpty {
                        ForEach(companiesFetchedResults) {
                            Text($0.companyName).tag($0 as Company?)
                        }
                    } else {
                        ForEach(companiesFetchedResults.filter {
                            $0.companyName.contains(searchText)
                        }) {
                            Text($0.companyName).tag($0 as Company?)
                        }
                    }
                }
            }
        }.navigationBarTitleDisplayMode(.inline)
    }

You need to explicitly set navigationBarTitleDisplayMode to inline, which should solve your problem:您需要将 navigationBarTitleDisplayMode 显式设置为内联,这应该可以解决您的问题:

.navigationBarTitleDisplayMode(.inline)

EDIT:编辑:

Why is there a huge big gap between the back button and the starting element of the view? 为什么后退按钮和视图的起始元素之间存在巨大的差距?

TL;DR: Do not embed pushed views in the NavigationView TL;DR:不要在 NavigationView 中嵌入推送视图

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

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