swift - NSGridView 困难

标签 swift macos nsview

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

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 填满了整个 View ,但复选框确实关闭了。正如你在图片中看到的那样。

此外,我希望内容不会填满整个单元格,让所有内容看起来更居中。

我该如何解决这个问题?

enter image description here

最佳答案

如果将行对齐更改为 .lastBaseline,它将解决 NSButtonNSTextField 的垂直对齐问题。如果你想要更多的间距(我猜这就是你的意思“我希望内容不会填满整个单元格”),你可以使用 columnSpacingrowSpacing NSGridView 的属性。如果您随后还在“真实”内容周围添加 NSGridCell.emptyContentView 实例,您应该能够实现以下效果。 enter image description here

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

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)
    }
}

关于swift - NSGridView 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045470/

相关文章:

swift - 使用 Swift 重构/组合多个几乎相同的类的推荐方法?

ios - 控制显示的 y 轴标签?

macos - 如何让 gitflow 允许创建多个修补程序?

objective-c - 带有 mask 的 NSVisualEffectView 有锯齿状的角

ios - 使用 Xcode 中另一个场景中的按钮暂停音乐

swift - Alamofire 请求已取消 (-999)

node.js - 在 Mac 上安装 Remotebuild 失败,并显示“uid 必须是无符号整数”

c++ - Emacs shell 与带有 GCC 的终端有不同的行为

objective-c - 部分隐藏的 NSView 上的鼠标进入/退出事件

swift - 从 CALayer 或 NSView 获取图像(swift 3)