【问题标题】:SwiftUI Nested NavigationView navigationBar disappearsSwiftUI Nested NavigationView 导航栏消失
【发布时间】:2019-10-10 19:48:29
【问题描述】:

我有一个三个视图,它们是列表。结构 MainMenuView:查看 { @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")
            }
        )
    )

}

发生的情况是:当我点击卖出时,我推动了该部分。但是,当它推入时,我看到了导航栏,然后它就被折叠了。但是,当我点击视图中的任何内容以触发视图重新加载时,它就会显示出来。

导航栏崩溃的原因是什么?

【问题讨论】:

    标签: swiftui ios-navigationview


    【解决方案1】:

    在 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)
    }
    

    【讨论】:

      【解决方案2】:

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

      您可以将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())
          }
      }
      

      【讨论】:

        【解决方案3】:

        Arrrgh...这是不必要的:MatchupView 中的 .navigationBarHidden(true)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-06
          • 1970-01-01
          • 1970-01-01
          • 2020-04-11
          • 2020-09-12
          • 2019-10-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多