代码之家  ›  专栏  ›  技术社区  ›  user4992124

NSGridView困难

  •  3
  • user4992124  · 技术社区  · 6 年前

    我用的是 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视图 填充整个视图,但复选框实际上是关闭的。如图所示。

    我该怎么解决?

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  4
  •   silicon_valley    6 年前

    如果将行对齐方式更改为 .lastBaseline 它会解决你的问题 NSButton NSTextField . 如果你想要更多的间隔(我猜这就是你所说的“我希望内容不能填满整个单元格”),你可以使用 columnSpacing rowSpacing 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)
        }
    }
    
        2
  •  0
  •   Victor Apeland    6 年前

    NSGridView ,我建议使用 NSTableView .