SwiftUI NavigationBar在滚动时不消失 [英] SwiftUI NavigationBar not disappearing while scrolling

查看:230
本文介绍了SwiftUI NavigationBar在滚动时不消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在滚动时隐藏我的NavigationBar,实际上它必须自动隐藏,但是当我尝试使用多个视图时它不起作用.另外,当我删除自定义视图并用NavigationView封装List时,它也可以工作.但是我需要SearchBarStatusView视图.有什么建议吗?

I want to hide my NavigationBar while scrolling, actually It must hide automatically but when I tried with multiple views It doesn't work. Also, It works when I remove custom views and capsulate List with NavigationView. But I need SearchBar and StatusView view. Is there any suggestion?

顺便说一下,我在设备上运行它,在这里我使用canvas进行演示.

By the way, I run it on the device, I use canvas here for demonstration purposes.

谢谢.

var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                SearchBar(searchText: $viewModel.searchText)
                StatusView(status: $viewModel.status)
                Divider()
                List(0...viewModel.characters.results.count, id: \.self) { index in
                    if index == self.viewModel.characters.results.count {
                        LastCell(vm: self.viewModel)
                    } else {
                        ZStack {
                            NavigationLink(destination: DetailView(detail: self.viewModel.characters.results[index])) {
                                EmptyView()
                            }.hidden()
                            CharacterCell(character: self.viewModel.characters.results[index])
                        }
                    }
                }
                .navigationBarTitle("Characters", displayMode: .large)
            }

        }
        .onAppear {
            self.viewModel.getCharacters()
        }
    }

推荐答案

好主意,抓痒...尝试将您的自定义视图放入List内,如下所示(我知道它会起作用,但是我不确定是否自动隐藏将起作用)

Just idea, scratchy... try to put your custom views inside List as below (I know it will work, but I'm not sure if autohiding will work)

NavigationView {
  List {
      SearchBar(searchText: $viewModel.searchText)
      StatusView(status: $viewModel.status)
      Divider()

      ForEach (0...viewModel.characters.results.count, id: \.self) { index in
      ...

这篇关于SwiftUI NavigationBar在滚动时不消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆