BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: .inspector initial width

Forums > SwiftUI

I'm building an app for the Mac that includes an inspector pane. I want to set the initial width of the inspector when it gets opened, but am failing miserably. The inspector is defined in code as:

            NavigationSplitView(columnVisibility: $columnsVisible,
                                sidebar: {
                SidebarView(vm: vm, detailView: $detailViewStyle)
                    .frame(minWidth: Constants.mainWindowSidebarMinWidth)
            }, detail: {
                detailView()
                    .inspector(isPresented: $showInspector) {
                        notePreviewView()
                            .inspectorColumnWidth(min: 250, ideal: 450, max: 900)
                    }
            })

As you can see, I have set minimum, ideal and maximum sizes. My expectation was that it would open to the ideal size but the inspector actually opens to the minimum size. My plan would be to save the inspector width and restore it when the app restarts but that's not going to work if I can't set the initial size.

I've triied setting the width of the notePreviewView and that will force the inspector to the correct size. However, it also makes the view a fixed width. I need to be able to resize the preview.

Does anyone know how to set the initial size of the inspector to the ideal width and keep the ability to change the width?

Thanks Steve

Inspector

1      

@FranG  

I've recently been doing something similar and ran into the same issue where the ideal width was ignored. It isn't clear to me under which circumstances that parameter is used, but I was able to get the right initialization behavior by (1) tracking whether the view has appeared and (2) selectively applying a frame minWidth until that happens. I'm not super-thrilled at this nonsense, but this is lifted from what I'm doing:

  @State private var hasInspectorAppeared: Bool = false

  var body: some View {
     NavigationSplitView {
            NavigatorView()
                .navigationSplitViewColumnWidth(min: 200, ideal: 300, max: 350)
                .background(.thinMaterial)
  } detail: {
      EditorView()
        .inspector(isPresented: $isShowingInspector) {
                InspectorView(userState: document.userState)
                        .inspectorColumnWidth(min: 200, ideal: self.inspectorIdealWidth, max: 350)
                        .interactiveDismissDisabled()
                        .frame(minWidth: hasInspectorAppeared ? nil : self.inspectorIdealWidth)
                        .onAppear(perform: {
                            hasInspectorAppeared = true
                        })
        }
      }
  }

  private var inspectorIdealWidth: CGFloat { 
      // ...loaded from your restoration data or some default
      return 300
   }

1      

Thank you so much for this, I would never have got there! Honestly, it feels like a bug to me but not one that I would expect to get fixed quickly. This will take away the irritation I feel every time I fire up the app.

1      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.