简体   繁体   中英

SwiftUI macOS app changes sidebar height on focus?

I'm having an issue with the Sidebar in a SwiftUI macOS app. When the app is first launched, without focus, there is an extra top padding in the sidebar, which gets collapsed to the correct size, when the window gets focus. This seems like a bug to me. Any way to fix this, or am I doing something wrong?

Before (No Focus)

在此处输入图像描述

After (Focus)

在此处输入图像描述

As you can see, the space between "Inbox" and the three traffic light dots are different in these two cases.

在此处输入图像描述

Code

Here is the code for the sidebar:

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

This looks like effect of NavigationView titlebar, try to make it empty explicitly, like

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

Update another idea - give explicit list row insets

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

I figured it out with some help. Here is a working solution.

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

The key part is the .edgesIgnoringSafeArea(.all) on the destination view. It also has to be specified at that "level" in the view hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM