我正在开发一个代码太多的复杂应用程序,但我try 创建了一个包含必要运行代码的示例应用程序来解释我的问题.

假设我有一个带有按钮的内容视图,该按钮打开一个带有导航标题、搜索栏和按钮的模式/工作表(名为"Main View").在这个主视图中,搜索栏和导航标题被正确定位.当我用主视图点击按钮时,它会导航到另一个有搜索栏和导航标题的视图(名为"Sub View"),并最终会有一个后退按钮来导航回主视图.在这个子视图中,我看到导航标题和搜索栏的位置不正确,即导航标题和搜索栏之间有很多空间.

我不能找出它的原因和如何修复它.对此有什么 idea 或 idea 吗?是因为两个导航堆栈,即一个在SampleAppApp struct 中,另一个在MainView struct 中吗?

100

import SwiftUI
import Foundation
import Combine

@main
struct SampleAppApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationStack {
                ContentView()
            }
        }
    }
}

struct ContentView: View {
    @State private var showModal: Bool = false
    var body: some View {
        VStack {
            Button("Click ME", action: { self.showModal.toggle() })
        }
        .sheet(isPresented: $showModal) {
            ZStack {
                Color(red: 0.95, green: 0.95, blue: 0.97)
                MainView(isPresented: $showModal).padding(.top, 15.0)
            }
        }
    }
}

// MARK: Code for Main View after "Click ME" is clicked
struct MainView: View {
    @State private var searchText = ""
    let searchTextPublisher = PassthroughSubject<String, Never>()

    /// Set to true when the panel is displayed. Dismisses the panel when set to false.
    var isPresented: Binding<Bool>
    @State private var showSubView: Bool = false

    var body: some View {
        NavigationStack {
            ZStack {
                Color(red: 0.95, green: 0.95, blue: 0.97).edgesIgnoringSafeArea(.all)
                VStack {
                    VStack {
                        Button("Tap to open sub-view", action: {
                            self.showSubView.toggle()
                        }).padding(.top, 20.0)
                    }
                    .navigationDestination(isPresented: $showSubView) {
                        SubView()
                    }
                    
                    MainViewSearchResultListAndNavTitle()
                    .searchable(
                        text: $searchText,
                        placement: .navigationBarDrawer(displayMode: .always),
                        prompt: "search"
                    )
                    .onChange(of: searchText) { newText in
                        searchTextPublisher.send(newText) /// Publishes when a search term is changed. This is used to debounce the search.
                    }
                    Spacer()
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color(red: 0.95, green: 0.95, blue: 0.97))
            }
        }
        .presentationDetents([.large])
        .interactiveDismissDisabled(true)
        .presentationBackgroundInteraction(.enabled)
    }
}

/// Shows search bar and search results list.
struct MainViewSearchResultListAndNavTitle: View {
    var body: some View {
        VStack {
            // Contains search results list
        }
        .toolbar(content: {
            ToolbarItem(placement: .navigationBarLeading) {
                Text("Main View")
                    .bold()
                    .font(.largeTitle)
                    .padding(.bottom, 15.0)
            }
        })
        .toolbarBackground(Color(red: 0.95, green: 0.95, blue: 0.97), for: .navigationBar)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}

// MARK: Code for Sub View after "Tap to open sub-view" is clicked. This view will have back button to go back to 2nd view i.e. "Main View".
struct SubView: View {
    var body: some View {
        ZStack {
            Color(red: 0.95, green: 0.95, blue: 0.97).edgesIgnoringSafeArea(.all)
            VStack {
                SubViewSearchResultsListAndNavTitle()
                .searchable(
                    text: .constant(""),
                    placement: .navigationBarDrawer(displayMode: .always),
                    prompt: "search"
                )
                Spacer()
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color(red: 0.95, green: 0.95, blue: 0.97))
        }
    }
}

private struct SubViewSearchResultsListAndNavTitle: View {
    var body: some View {
        VStack {
            // Contains search results list for sub view.
        }
        .toolbar(content: {
            ToolbarItem(placement: .principal) {
                Text("Sub view").bold()
                    .font(.title)
                    .padding(.bottom, 15.0)
            }
        })
        .toolbarBackground(Color(red: 0.95, green: 0.95, blue: 0.97), for: .navigationBar)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationBarBackButtonHidden(true)
    }
}

100

enter image description here

推荐答案

使用Xcode的UI调试器,我发现空格实际上是_UINavigationBarLargeTitle,所以这个空格实际上是用来显示大导航标题的,除非您没有设置navigationTitle,所以它什么都不显示.

SubView上使用.inline标题样式可以删除空格.

.navigationDestination(isPresented: $showSubView) {
    SubView().navigationBarTitleDisplayMode(.inline)
}

不过,我不知道为什么你不需要为MainView英镑做同样的事情.也许这是.automatic风格中的一些隐藏的逻辑.

Ios相关问答推荐

如何在进行滑动删除操作时保持自定义视图背景静态

如何在SWIFT中处理不可发送的类型?

如何在PINCH上添加UITextfield以zoom 图像

在带有可编码的SWIFT5中,可以轻松处理以美元符号开头的JSON密钥,例如$DATE";

RxSwift:如何使用另外两个可观测对象(其中一个依赖于另一个)创建可观测对象?

在相机动作之前快速防止场景视点重置

UITableViewCell编程约束问题

如何在后台显示 Swift UI 中的通讯通知?

如何根据条件快速将返回类型设为 LinearGradient 或 AngularGradient?

如何在swiftui中更改可搜索的搜索图标 colored颜色

带有属性包装器的不可用属性

在 iPad 上删除列表的顶部和底部行

iOS 11 navigationItem.titleView 宽度未设置

使用导航控制器在prepareForSegue中设置数据

Sierra 中的安全/协同设计: keys 串忽略访问控制设置和 UI 提示权限

如何在 Android 和 iPhone 的移动应用程序中实施推荐计划

Objective-C 将 NSDate 设置为当前 UTC

为给定的 UIColor 获取更亮和更暗的 colored颜色 变化

Xcode 5 和 iOS 7:架构和有效架构

我可以在 UIScrollView 中使用 UIRefreshControl 吗?