【问题标题】:SwiftUI macOS app changes sidebar height on focus?SwiftUI macOS 应用程序在焦点上更改侧边栏高度?
【发布时间】:2020-11-30 15:12:44
【问题描述】:

我在 SwiftUI macOS 应用程序中遇到了边栏问题。当应用程序首次启动时,没有焦点,侧边栏中有一个额外的顶部填充,当窗口获得焦点时,它会折叠到正确的大小。这对我来说似乎是一个错误。有什么办法可以解决这个问题,还是我做错了什么?

之前(无焦点)

之后(焦点)

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

代码

这是侧边栏的代码:

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

【问题讨论】:

    标签: swift macos swiftui


    【解决方案1】:

    这看起来像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))
    

    【讨论】:

    • 很好的建议,但仍未解决问题。 :(
    【解决方案2】:

    我在一些帮助下想通了。这是一个可行的解决方案。

    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)。它还必须在视图层次结构中的那个“级别”上指定。

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 2012-07-20
      相关资源
      最近更新 更多