【问题标题】:NSToolbar Flexible space not working in Swift 5.xNSToolbar 灵活空间在 Swift 5.x 中不起作用
【发布时间】:2020-08-19 16:05:00
【问题描述】:

我使用 Xcode 10.1(我相信是 Swift 4.2)开发了这个应用程序,工具栏看起来不错:

但是我移植到我的 Xcode 11.6(我相信是 Swift 5.2),现在工具栏上最右边的项目不再位于最右边:

在移植之前,我添加了加号按钮 - 复选框(显示隐藏)是第一个项目之一,此后从未更改。 按钮也按预期工作 - 他们的动作可以触发。 注意:此应用不使用 Storyboard 或 IB 东西。

由于我的代码拆分为多个文件(包含许多不相关的代码),我将给出一个概述。

class ToolbarController: NSObject, NSToolbarDelegate {

let toolbar = NSToolbar(identifier: NSToolbar.Identifier("toolbar"))
lazy var toolbarItem1: NSToolbarItem = {
    let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "Btn1"))
    let button = NSButton(frame: NSRect(x: 0, y: 0, width: 40, height: 1))
    button.target = self
   ...
    toolbarItem.view = button
    return toolbarItem
}() // defined as "lazy" to allow "self"
var toolbarItemSpace: NSToolbarItem = {
    let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.space)
    return toolbarItem
}()
 ...
var toolbarItemFlexSpace: NSToolbarItem = {
    let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.flexibleSpace)
    return toolbarItem
}()
lazy var toolbarItemShowHidden: NSToolbarItem = {
    let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier(rawValue: "Checkbox"))
    let box = NSBox(frame: NSRect(x: 0, y: 0, width: 120, height: 40))
    let button = NSButton() // put in NSBox to use isolated action
  ...
    button.target = self
    box.contentView = button
    box.sizeToFit() // tightly fit the contents (button)
    toolbarItem.view = box
    return toolbarItem
}()
lazy var tbItems: [NSToolbarItem] = [toolbarItem1, ...]

该类定义工具栏、添加项目、实现所有这些工具栏(委托)方法并使其成为委托。然后我的 NSWindowController 将其设置为应用程序工具栏。

任何人都可以看到导致上述问题的我的代码问题吗? 否则,这是一个错误吗?

【问题讨论】:

标签: swift xcode macos nstoolbar nstoolbaritem


【解决方案1】:

在 Swift 5.0 上遇到了同样的问题......即使文档说 minSize 和 maxSize 已被弃用,我还是解决了这个问题:

let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.flexibleSpace)
toolbarItem.minSize = NSSize(width: 1, height: 1)
toolbarItem.maxSize = NSSize(width: 1000, height: 1) //just some large value
return toolbarItem

也许这也适用于 Swift 5.2

【讨论】:

    【解决方案2】:

    找到了一种方法,无需使用警告且不使用已弃用的属性。

    我们为 NASToolbarItem 预先创建了一个透明的子视图,带有最小/最大约束,因此系统可以自行计算最小和最大尺寸。

    let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.flexibleSpace)
    
    // view to be hosted in the flexible space toolbar item
    let view = NSView(frame: CGRect(origin: .zero, size: CGSize(width: MIN_TOOLBAR_ITEM_W, height: MIN_TOOLBAR_H)))
    view.widthAnchor.constraint(lessThanOrEqualToConstant: MAX_TOOLBAR_ITEM_W).isActive = true
    view.widthAnchor.constraint(greaterThanOrEqualToConstant: MIN_TOOLBAR_ITEM_W).isActive = true
    
    // set the view and return
    toolbarItem?.view = view
    return toolbarItem
    

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多