【问题标题】:NSGridView difficultiesNSGridView 的困难
【发布时间】:2018-08-27 18:58:47
【问题描述】:

我在我的 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 填满了整个视图,但复选框确实处于关闭状态。正如您在图像中看到的那样。

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

我该如何解决这个问题?

【问题讨论】:

    标签: swift macos nsview


    【解决方案1】:

    如果您将行对齐更改为.lastBaseline,它将解决您的NSButtonNSTextField 的垂直对齐问题。如果您想要更大的间距(我猜这就是您所说的“我希望内容不填满整个单元格”的意思),您可以使用NSGridViewcolumnSpacingrowSpacing 属性。如果您随后还在您的“真实”内容周围添加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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-16
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多