1

我有一个NavigationView.navigationBarHidden(true)在我的SplashScreen. 在这里它没有正确显示,但是当我进入下一个屏幕时,会NavigationView出现该栏。如何正确隐藏导航栏?背景也无法正确显示。

看法

struct EventsScreen: View {

    var eventsRepository: EventsRepository

    @State
    var currentPage: Int = 0
    @State
    private var searchTerm : String = ""


    func getEventSections() -> [EventSection] {
        eventsRepository.fetchEventSections()
    }

    func getViewControllers() -> [UIHostingController<EventFeatureView>] {
        return eventsRepository.fetchFeaturedEvents().map({ event in
            UIHostingController(rootView: EventFeatureView(event: event))
        })
    }
    var body: some View {
        NavigationView {
            List {
                ZStack(alignment: .top) {

                    EventViewController(controllers: self.getViewControllers(), currentPage: self.$currentPage)
                    VStack {
                        SearchBar(text: $searchTerm)
                            .padding(EdgeInsets.init(top: 16, leading: 16, bottom: 0, trailing: 16))
                        HStack {
                            Spacer()
                            Chip(text: "Dates", action:  {
                                //TODO filter on dates
                            })
                            Chip(text:"Type", action: {
                                //TODO filter event type
                            })
                            Chip(text: "Points", action: {
                                //TODO filter points
                            })
                            Spacer()
                        }
                    }
                }.listRowInsets(EdgeInsets())
                    .frame(height: 600)
                ForEach(self.getEventSections()) { section in
                    EventSectionView(eventSection: section)
                }
            }
        }
        .background(LinearGradient(gradient: Gradient(colors: [.black, ColorTheme.brandPurple.color]), startPoint: .top, endPoint: .bottom))
        .navigationBarTitle(Text("Events"), displayMode: .inline)
    .navigationBarHidden(true)
    }
}
4

3 回答 3

1

您可以为正在呈现.navigationBarHidden(true)的孩子设置. 我不确定下一个屏幕会是什么样子,但我会尝试以下方法:ViewNavigationViewView

struct NextScreen: View {
    var body: some View {
        Group {
            // existing body content
        }
        .background(LinearGradient(gradient: Gradient(colors: [.black, ColorTheme.brandPurple.color]), startPoint: .top, endPoint: .bottom))
        .navigationBarTitle(Text("Next Screen"), displayMode: .inline)
        .navigationBarHidden(true)
    }
}

Group以防万一您的正文内容未全部包含在某种(View例如VStackList等)中。

像这样考虑它可能会有所帮助:

NavigationView通常会.navigationBarHidden()从其子视图继承。但是,由于.navigationBarHidden()和是在in.background()之外显式定义的,它们碰巧会向下工作并应用于子视图 in (除非这些视图具有自己显式定义的属性)。NavigationViewEventsScreenEventsScreen

虽然NextScreenView 仍然显示在 中NavigationView,但NextScreen它使用自己的默认属性进行初始化,例如背景颜色。一旦应用程序导航/更新到NextScreen, 的属性NextScreen将优先,包括默认值.navigationBarHidden(false),并且可能是系统背景颜色。这是关于哪个视图(及其属性)将成为先例,SwiftUI 倾向于给子级优先权,并从那里扩展(假设给定属性的范围适用于其父级视图)。

NavigationView因此,总而言之,如果您希望它们保持更改,则必须在出现在 中的每个视图中显式覆盖这些默认值。

于 2020-03-05T02:36:54.787 回答
0

您需要像这样使用您的代码:

var body: some View {
    NavigationView {
        ...
        .navigationBarTitle(Text("Events"), displayMode: .inline)
        .navigationBarHidden(true)
    }
    // that means only show one view at a time no matter what device I'm working
    .navigationViewStyle(StackNavigationViewStyle())
}

您可以绑定navigationBarHidden到变量,以便您可以在某些条件下更改值。像这样:.navigationBarHidden($onOff)

于 2021-04-09T18:03:59.210 回答
0

这是 SDK 所说的:

/// Hides the navigation bar for this view.
///
/// This modifier only takes effect when this view is inside of and visible
/// within a `NavigationView`.
///
/// - Parameters:
///     - hidden: A Boolean value that indicates whether to hide the
///       navigation bar.
@available(OSX, unavailable)
public func navigationBarHidden(_ hidden: Bool) -> some View

因此,您必须为导航期间显示false每个顶视图设置它NavigationView,否则它将在您观察时显示。

于 2020-03-04T05:51:27.870 回答