0

我想使用 Interface Builder 使用的 NSMatrix 方法创建一组单选按钮,但在代码中。矩阵使用自动布局进行布局。我大部分时间都在工作,除了在运行时添加新选项时。

在下面的示例中,单击 Append Item 几次就可以正常工作,然后矩阵开始从靠近顶部的窗口中移出(至少我认为它在顶部被剪裁了)。如果在添加一堆项目后最大化此窗口,则窗口将保持相同的高度,并且所有项目将被裁剪到每个像素高,这是非常不受欢迎的事情:)

在我的真实程序(不是下面的这个测试)中,它工作得很好,但是如果我动态添加一个选项,在一定数量的项目(最初是 5 个)之后,选项将非常轻微地剪辑,出现轻微的挤压或挤压。添加另一个选项会恢复此状态,直到命中下一个幻数。

这是怎么回事?我正在 OS X Yosemite 上对此进行测试。谢谢。

// 17 august 2015
import Cocoa

var keepAliveMainwin: NSWindow? = nil
var matrix: NSMatrix? = nil

class ButtonHandler : NSObject {
    @IBAction func onClicked(sender: AnyObject) {
        var lastRow = matrix!.numberOfRows
        matrix!.renewRows(lastRow + 1, columns: 1)
        var cell = matrix!.cellAtRow(lastRow, column: 0) as! NSButtonCell
        cell.title = "New Item"
        matrix!.sizeToCells()
    }
}

var buttonHandler: ButtonHandler = ButtonHandler()

func appLaunched() {
    var mainwin = NSWindow(
        contentRect: NSMakeRect(0, 0, 320, 240),
        styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
        backing: NSBackingStoreType.Buffered,
        defer: true)
    var contentView = mainwin.contentView as! NSView

    var prototype = NSButtonCell()
    prototype.setButtonType(NSButtonType.RadioButton)
    prototype.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))

    matrix = NSMatrix(frame: NSZeroRect,
        mode: NSMatrixMode.RadioModeMatrix,
        prototype: prototype,
        numberOfRows: 0,
        numberOfColumns: 0)
    matrix!.allowsEmptySelection = false
    matrix!.selectionByRect = true
    matrix!.intercellSpacing = NSMakeSize(4, 2)
    matrix!.autorecalculatesCellSize = true
    matrix!.drawsBackground = false
    matrix!.drawsCellBackground = false
    matrix!.autosizesCells = true
    matrix!.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(matrix!)

    var button = NSButton(frame: NSZeroRect)
    button.title = "Append Item"
    button.setButtonType(NSButtonType.MomentaryPushInButton)
    button.bordered = true
    button.bezelStyle = NSBezelStyle.RoundedBezelStyle
    button.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
    button.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(button)

    button.target = buttonHandler
    button.action = "onClicked:"

    var views: [String: NSView]
    views = [
        "button":   button,
        "matrix":   matrix!,
    ]
    addConstraints(contentView, "V:|-[matrix]-[button]-|", views)
    addConstraints(contentView, "H:|-[matrix]-|", views)
    addConstraints(contentView, "H:|-[button]-|", views)

    mainwin.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
    mainwin.makeKeyAndOrderFront(mainwin)
    keepAliveMainwin = mainwin
}

func addConstraints(view: NSView, constraint: String, views: [String: NSView]) {
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
        constraint,
        options: NSLayoutFormatOptions(0),
        metrics: nil,
        views: views)
    view.addConstraints(constraints)
}

class appDelegate : NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(note: NSNotification) {
        appLaunched()
    }

    func applicationShouldTerminateAfterLastWindowClosed(app: NSApplication) -> Bool {
        return true
    }
}

func main() {
    var app = NSApplication.sharedApplication()
    app.setActivationPolicy(NSApplicationActivationPolicy.Regular)
    // NSApplication.delegate is weak; if we don't use the temporary variable, the delegate will die before it's used
    var delegate = appDelegate()
    app.delegate = delegate
    app.run()
}

main()
4

1 回答 1

1

显然,您需要省略对sizeToCells()after 的调用renewRows(_:columns:)。我的猜测是它设置了框架大小,这在使用自动布局时几乎没有用,但也会在某处清除一个“脏”标志,告诉矩阵它需要使其内在大小无效。换句话说,矩阵认为它已经完成了它需要做的重新布局工作。

于 2015-08-23T23:31:31.120 回答