首页 > 解决方案 > NSGridView 的困难

问题描述

我在NSGridView我的 macOS 应用程序的设置面板中使用了一个。我是这样设置的:

class GeneralViewController: RootViewController {
    private var gridView: NSGridView?

    override func loadView() {
        super.loadView()

        let restaurantLabel = NSTextField()
        restaurantLabel.stringValue = "Restaurant"

        let restaurantButton = NSPopUpButton()
        restaurantButton.addItems(withTitles: ["A", "B", "C"])

        let updatesLabel = NSTextField()
        updatesLabel.stringValue = "Updates"
        updatesLabel.isEditable = false
        updatesLabel.isBordered = false
        updatesLabel.isBezeled = false

        let updatesButton = NSButton(checkboxWithTitle: "Automatically pull in updates from WordPress", target: nil, action: nil)

        let empty = NSGridCell.emptyContentView
        gridView = NSGridView(views: [
            [restaurantLabel, restaurantButton],
            [updatesLabel, updatesButton]
            ])
        gridView?.wantsLayer = true
        gridView?.layer?.backgroundColor = NSColor.red.cgColor
        gridView?.column(at: 0).xPlacement = .trailing
        gridView?.rowAlignment = .firstBaseline
        gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .horizontal)
        gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .vertical)

        view.addSubview(gridView!)
    }

    override func viewDidLayout() {
        super.viewDidLayout()

        gridView?.frame = NSRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
    }
}

现在,当我运行它时,它会NSGridView填满整个视图,但复选框确实是关闭的。正如您在图像中看到的那样。

另外,我希望内容不要填满整个单元格,让它看起来更集中一点。

我该如何解决这个问题?

在此处输入图像描述

标签: swiftmacosnsview

解决方案


如果您将行对齐更改为.lastBaseline它将解决您的垂直对齐问题NSButtonNSTextField. 如果您想要更多间距(我猜这就是“我希望内容不填满整个单元格”的意思),您columnSpacing可以rowSpacing使用NSGridView. 如果你然后NSGridCell.emptyContentView在你的“真实”内容周围添加实例,你应该能够达到以下效果。 在此处输入图像描述

这是我建议的更改后的新代码:

class GeneralViewController: RootViewController {
    private var gridView: NSGridView?

    override func loadView() {
        super.loadView()

        let restaurantLabel = NSTextField()
        restaurantLabel.stringValue = "Restaurant"

        let restaurantButton = NSPopUpButton()
        restaurantButton.addItems(withTitles: ["A", "B", "C"])

        let updatesLabel = NSTextField()
        updatesLabel.stringValue = "Updates"
        updatesLabel.isEditable = false
        updatesLabel.isBordered = false
        updatesLabel.isBezeled = false

        let updatesButton = NSButton(checkboxWithTitle: "Automatically pull in updates from WordPress", target: nil, action: nil)

        let empty = NSGridCell.emptyContentView
        gridView = NSGridView(views: [
            [empty],
            [empty, restaurantLabel, restaurantButton, empty],
            [empty, updatesLabel, updatesButton, empty],
            [empty],
            [empty]
        ])
        gridView?.wantsLayer = true
        gridView?.layer?.backgroundColor = NSColor.red.cgColor
        gridView?.column(at: 0).xPlacement = .trailing
        gridView?.rowAlignment = .lastBaseline
        gridView?.columnSpacing = 20
        gridView?.rowSpacing = 20
                gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .horizontal)
        gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .vertical)

        view.addSubview(gridView!)
    }

    override func viewDidLayout() {
        super.viewDidLayout()

        gridView?.frame = NSRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
    }
}

推荐阅读