Hello, everyone.

I've been making an app looks like the default Notes app.
To implement the sidebar view, I've used the NavigationSplitView, and tried to control its columnVisibility. However, it doesn't work.

// some pseudo code
.onAppear {
  columnVisibility = savedColumnVisibility
}
.onChange(of: columnVisibility) { value in
  savedColumnVisibility = value
}
.onChange(of: scenePhase) { phase in
  if phase == .activate {
    load(savedColumnVisibility)
  } else if phase == .background {
    save(savedColumnVisibility)
  }
}

Is there any way for me to persist the columnVisibility of NavigationSplitView?

Notes app can persist the columnVisibility:

However, my app can't persist it:

Thanks to watch my post!

I solved it by myself, and share it with you.

.onAppear {
  columnVisibility = savedColumnVisibility
}
.onChange(of: scenePhase) { phase in
  if phase == .active {
    isActive = true
  } else if phase == .inactive {
    if isActive {
      save(columnVisibility)
      isActive = false
    } else {
      columnVisibility = savedColumnVisibility
    }
  }
}

The main point is that the inactive phase is a two-directional phase. So, I use only the inactive phase to save and restore the columnVisibility.

I don't know what is the best solution, but it works.
All the other solutions, such as using the background phase, or using an AppStorage or an SceneStorage, doesn't work well.