使用SwiftUI时调整NSSplitViewController的大小

问题描述 投票:0回答:1

我正在尝试在SwiftUI中使用NSSplitViewController,并具有一个简单的主视图和详细视图,如下面的代码片段所示。效果很好,但是在调整窗口大小时,SplitView无法展开,而在视图调试器中,我看到几何具有调整后窗口的尺寸。

如何根据窗口尺寸(或GeometryReader)调整SplitView的大小。

struct SampleView: View {
    @EnvironmentObject var env : AppEnvironment

    var body: some View {
        Group {
            if (self.env.files.isEmpty) {
                Text("loading")
            } else {
                GeometryReader { geometry in
                    SplitView(master: {
                        Text("master")
                            .background(Color.yellow)
                    }, detail: {
                        Text("Detail")
                            .background(Color.orange)
                    }).frame(width: geometry.size.height, height: geometry.size.height)
                }
            }
        }
    }
}

下面是SplitView的实现

//
// Source: https://gist.github.com/HashNuke/f8895192fff1f275e66c30340f304d80
//
struct SplitView<Master: View, Detail: View>: View {
    var master: Master
    var detail: Detail

    init(@ViewBuilder master: () -> Master, @ViewBuilder detail: () -> Detail) {
        self.master = master()
        self.detail = detail()
    }

    var body: some View {
        let viewControllers = [NSHostingController(rootView: master), NSHostingController(rootView: detail)]
        return SplitViewController(viewControllers: viewControllers)
    }
}

struct SplitViewController: NSViewControllerRepresentable {
    var viewControllers: [NSViewController]

    private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

    func makeNSViewController(context: Context) -> NSViewController {
        let controller = NSSplitViewController()

        controller.splitView.dividerStyle = .thin
        controller.splitView.autosaveName = NSSplitView.AutosaveName(splitViewResorationIdentifier)
        controller.splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)
        let vcLeft = viewControllers[0]
        let vcRight = viewControllers[1]
        vcLeft.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true
        vcRight.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 70).isActive = true
        let sidebarItem = NSSplitViewItem(contentListWithViewController: vcLeft)
        sidebarItem.canCollapse = false

        // I'm not sure if this has any impact
        // controller.view.frame = CGRect(origin: .zero, size: CGSize(width: 800, height: 800))
        controller.addSplitViewItem(sidebarItem)

        let mainItem = NSSplitViewItem(viewController: vcRight)
        controller.addSplitViewItem(mainItem)

        return controller
    }

    func updateNSViewController(_ nsViewController: NSViewController, context: Context) {
        print("should update splitView", nsViewController.view.superview?.frame, nsViewController.view.frame)
    }
}
swift macos cocoa swiftui appkit
1个回答
0
投票

问题不是在拆分视图中,而是在内容子视图中,默认情况下,由于SwiftUI视图被限制为内容,因此以下内容

SplitView(master: {
    Text("master")  // Master content view
        .frame(maxWidth: .infinity, maxHeight: .infinity) // << here !!
        .background(Color.yellow)
}, detail: {
    Text("Detail") // Details content view
        .frame(maxWidth: .infinity, maxHeight: .infinity)  // << here !!
        .background(Color.orange)
})

给出了预期的结果

demo

注意:我使用了.frame(width: geometry.size.width, height: geometry.size.height),`因为认为这是一个错字。

© www.soinside.com 2019 - 2024. All rights reserved.