我正在开发一个代码太多的复杂应用程序,但我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