SwiftUI macOS应用程序在焦点变化时更改侧边栏的高度?

6

我在使用SwiftUI开发macOS应用时遇到了侧边栏的问题。当应用程序第一次启动且没有焦点时,侧边栏顶部会有额外的空白填充,但在窗口获得焦点时会折叠到正确的大小。这对我来说似乎是一个bug。有没有什么方法可以解决这个问题,或者是我的做法有问题?

之前(没有焦点)

enter image description here

之后(焦点)

enter image description here

如您所见,在这两种情况下,“收件箱”和三个信号灯之间的空格不同。

enter image description here

代码

以下是侧边栏的代码:

NavigationView {
    List {
      Text("Inbox")
      
      Section(header: Text("All projects")) {
        ForEachStore(
          self.store.scope(state: { $0.projects }, action: AppAction.project(id:action:)), content: { projectStore in
            WithViewStore(projectStore) { projectViewStore in
              NavigationLink(
                destination: ProjectView(store: projectStore),
                label: {
                  Text(projectViewStore.title)
                })
            }
          }
        )
        Spacer()
      }
    }.frame(minWidth: 160)
    .listStyle(SidebarListStyle())
    HStack {     
      HStack {
        Button("−") { viewStore.send(.decrementButtonTapped) }
        Text("\(viewStore.count)")
        Button("+") { viewStore.send(.incrementButtonTapped) }
        
      }
    } 
  }
2个回答

0

这看起来像是 NavigationView 标题栏的影响,尝试显式地将其置为空,例如:

}.frame(minWidth: 160, maxHeight: .infinity)     // also try this !!
.listStyle(SidebarListStyle())
.navigationTitle("")             // << here !!

更新:另一个想法 - 明确给出列表行插入

NavigationView {
    List {
      Text("Inbox")
         .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

好的建议,但仍然没有解决问题。 :( - eivindml

0

我在得到一些帮助后解决了这个问题。这是一个可行的解决方案。

NavigationView {
        List {
          Section(header: Text("All projects")) {
            ForEachStore(
              self.store.scope(
                state: { $0.projects },
                action: AppAction.project(id:action:)),
              content: { projectStore in
                NavigationLink(
                  destination: ProjectView(store: projectStore).edgesIgnoringSafeArea(.all),
                  label: {
                    EditableListItemView(store: projectStore)  
                  })
              }
            )
          }
        }
        .overlay(SidebarBottomView(store: store), alignment: .bottom)
        .frame(minWidth: 160, maxHeight: .infinity)
        .listStyle(SidebarListStyle())
        Text("Nothing here")
        
  }

关键部分是在目标视图上的.edgesIgnoringSafeArea(.all)。它还必须在视图层次结构的“级别”上指定。

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