UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.