Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a max width on a subview of HSplitView

Tags:

macos

swiftui

I'm having some trouble with HSplitView on macOS, and I'm not sure if it's a bug or something I'm doing wrong.

I'm trying to create an app with a [Sidebar | Main Content | Inspector] layout. This is similar to the SF Symbols app, where you can view more information about a selected icon.

The approach I've taken is to have the Main Content and Inspector views be in an HSplitView. Having three views in a NavigationView only seems to use the Mail pattern of [List | List | Detail], which is not the layout I'm looking for.

The problem I'm seeing is that when I set a maxWidth on the Inspector view and drag the divider to the left, the Main Content view resizes to be smaller than the available space. The parent HSplitView doesn't resize, so I'm not sure if I'm using the wrong modifiers or using them in the wrong places.

I've setup a basic test case on github. If y'all could offer and help or guidance, that would be amazing.

Observed behaviour GIF of the observed behaviour

Expected behaviour GIF of the expected behaviour

import SwiftUI

struct A: View {
    var body: some View {
        VStack {
            Spacer()
            
            HStack {
                Spacer()
                
                Text("Pane A")
                
                Spacer()
            }
            
            Spacer()
        }
        .frame(
            minWidth: 200,
            maxWidth: .infinity,
            maxHeight: .infinity,
            alignment: .leading
        )
        .background(Color.red)
        .foregroundColor(.black)
        .layoutPriority(1)
    }
}

struct B: View {
    var body: some View {
        VStack {
            Spacer()
            
            HStack {
                Spacer()
                
                Text("Pane B")
                
                Spacer()
            }
            
            Spacer()
        }
        .frame(
            minWidth: 200,
            idealWidth: 250,
            maxWidth: 300,
            maxHeight: .infinity
        )
        .background(Color.purple)
        .foregroundColor(.black)
    }
}

struct ContentView: View {
    var body: some View {
        HSplitView {
            A()
            B()
        }
        .frame(
            minWidth: 0,
            maxWidth: .infinity,
            maxHeight: .infinity
        )
        .background(Color.blue)
    }
}e

Thanks!

like image 614
Mike Avatar asked Nov 07 '22 01:11

Mike


1 Answers

I've had the same problem for several days, and I solved it in a flash.

Need to drag View

HSplitView {
    View1()
    View2()
}

Intuitively, we will set the maximum width of View1, but when SwiftUI drags, View1 exceeds the maximum width, it will display an inexplicable error

HSplitView {
    View1()
        .frame(maxWidth: 300)
    View2()
}

Solutions Set the minimum width of View2, and let View automatically fill all spaces. There will be no drag problem

HSplitView {
    View1()
    View2()
        .frame(minWidth: 500)
}
like image 140
WU_C Avatar answered Nov 14 '22 16:11

WU_C