【发布时间】:2018-02-11 01:06:15
【问题描述】:
我正在 macOS 上使用 Swift 编写应用程序。 我想创建一个带有垂直 NSStackView 的 NSAlert,让用户从 N 个选项中选择一个。为此,我将我的 NSStackView 连接到我的 NSAlert 的附件视图属性中,但由于某种原因,我的单选按钮没有显示。
这是我迄今为止尝试过的:
let a = NSAlert()
a.messageText = "Dummy1"
a.informativeText = "Dummy2"
a.addButton(withTitle: "OK")
let rb = NSButton(radioButtonWithTitle: "Foo", target: nil, action: nil)
a.accessoryView = rb
a.runModal()
这向我显示了我的 NSAlert,其中有一个标记为 Foo 的单选按钮。所以附件视图似乎起作用了。
现在我将单选按钮放在 StackView 中:
let a = NSAlert()
a.messageText = "Dummy1"
a.informativeText = "Dummy2"
a.addButton(withTitle: "OK")
let rb = NSButton(radioButtonWithTitle: "Foo", target: nil, action: nil)
let vsv = NSStackView()
vsv.orientation = NSUserInterfaceLayoutOrientation.vertical
vsv.distribution = NSStackViewDistribution.equalSpacing
vsv.alignment = .leading
vsv.isHidden = false
vsv.addView(rb, in: .center)
a.accessoryView = vsv
a.runModal()
现在单选按钮不再出现。在 StackView 中添加更多单选按钮也无济于事。
【问题讨论】:
-
一点点进展:如果我使用传递 NSRect 的构造函数创建我的 NSStackView(),那么我会看到内容,但它们被 NSRect 剪辑。我需要弄清楚如何让 NSStackView 扩展以适应其所有子视图。一直在玩压缩和拥抱优先事项,但到目前为止还没有成功。
标签: swift radio-button accessoryview nsalert nsstackview