SwiftUI嵌套的NavigationView导航栏消失

3

我有三个列表视图。结构体MainMenuView: View { @EnvironmentObject var dataModel:DM

var body: some View {

    return NavigationView{
        List {
            Matchup()
            GameSettings()
            EnteringGame()
        }
    }
}

在Matchup()函数内部

struct Matchup: View {
@EnvironmentObject var dataModel: DM    

var body: some View {
    Section(header: Text("MATCH-UP")
        .fontWeight(.heavy)
        .foregroundColor(Color("TPLightGrey"))
    ) {
        NavigationLink(destination: TrendSingleSelect(
            title: .constant("TEAM"),
            col: .constant(self.dataModel.queryColumnTeam1),
            items: .constant(self.dataModel.team1Values) ,
            selection: self.$dataModel.team1ListValue
        )) {
            HStack {
                Text("TEAM")
                Spacer()
                if dataModel.team1ListValue.count == 0 {
                    Text("IS ANY").foregroundColor(Color("TPLightGrey"))
                } else {
                    Text( self.dataModel.team1ListValue.joined(separator: ", ")).foregroundColor(Color("TPOrange"))
                }
            }
        }


    }
    .listRowBackground(Color("TPDarkGrey"))
    .font(.system(size: 14))
    .navigationBarTitle("", displayMode: .inline)
    .navigationBarHidden(true)
}

注意我隐藏了导航栏。当用户点击一行时,我想推入一个导航。这是最终的界面:

var body: some View {

    return VStack  {

        List {
            ForEach(self.items, id: \.self) { item in
                SingleSelectionRow(title: item, isSelected: self.selection.contains(item)) {

                    if self.selection.contains(item) {
                        self.selection = []
                    }
                    else {
                        self.selection = [item]

                    }
                    self.queryCallback()
                }
                .listRowBackground(Color("TPDarkGrey"))
            }//ForEach
        }//list
            .font(.system(size: 14))
    }

    .navigationBarHidden(false)
    .navigationBarTitle(title)
    .navigationBarItems(trailing:
        Button(action: {
               // Actions
                self.reset()
           }, label: {
            Text("Clear")
            }
        )
    )

}

发生的情况是:当我点击"sell"时,我推动了那个区域。然而,当它被推入时,我看到了navBar,然后它就被折叠了。但是,当我再次点击视图中的任何内容以触发视图重新加载时,它会出现。

是什么导致了navbar的折叠?

3个回答

0

在MatchupView中尝试使用这段代码:

struct Matchup: View {
@EnvironmentObject var dataModel: DM    

var body: some View {
NavigationView {            // attention hear************
    Section(header: Text("MATCH-UP")
        .fontWeight(.heavy)
        .foregroundColor(Color("TPLightGrey"))
    ) {
        NavigationLink(destination: TrendSingleSelect(
            title: .constant("TEAM"),
            col: .constant(self.dataModel.queryColumnTeam1),
            items: .constant(self.dataModel.team1Values) ,
            selection: self.$dataModel.team1ListValue
        )) {
            HStack {
                Text("TEAM")
                Spacer()
                if dataModel.team1ListValue.count == 0 {
                    Text("IS ANY").foregroundColor(Color("TPLightGrey"))
                } else {
                    Text( self.dataModel.team1ListValue.joined(separator: ", ")).foregroundColor(Color("TPOrange"))
                }
            }
        }


    }
    .listRowBackground(Color("TPDarkGrey"))
    .font(.system(size: 14))
}                // attention hear************
    .navigationBarTitle("", displayMode: .inline)
    .navigationBarHidden(true)
}

0

我无法编译您的项目,所以我假设了以下解决方案:

您可以将navigationBarHidden绑定到变量上,这样您就可以在特定条件下更改该值。就像这样:.navigationBarHidden($onOff)

struct ContentView: View {
    @State var onOff = false
    
    var body: some View {
        
        NavigationView {
            Button("Button") {
                self.onOff.toggle()
            }
            .navigationBarTitle(Text("Events"), displayMode: .inline)
            .navigationBarHidden($onOff.wrappedValue)
        }
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

-1

啊...这是不必要的:在MatchupView中使用.navigationBarHidden(true)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接