NSToolbarItem:"Make sure this toolbar item has a valid frame/min/max size"?

NSToolbarItem: "Make sure this toolbar item has a valid frame/min/max size"?

自从升级到 Mac OS Sierra 和新的 XCode 版本后,每次为每个 NSToolbarItems 启动我的应用程序时,我都会收到以下错误:

Example 1:
2016-09-29 12:46:58.659879 AppTest[] NSToolbarItem (<NSToolbarItem: >) had to adjust the size of <NSPopUpButton: > from {130, 26} to the expected size of {132, 27}. Make sure that this toolbar item view has a valid frame/min/max size. This is an app bug, please do not file a bug against AppKit or NSToolbar! Break on _NSToolbarAdjustedBorderedControlSizeBreakpoint
Example 2:
2016-09-29 12:46:58.666074 AppTest[] NSToolbarItem (<NSToolbarItem: >) had to adjust the size of <NSButton: > from {60, 25} to the expected size of {62, 27}. Make sure that this toolbar item view has a valid frame/min/max size. This is an app bug, please do not file a bug against AppKit or NSToolbar! Break on _NSToolbarAdjustedBorderedControlSizeBreakpoint

我尝试在 StoryBoard 中随意更改大小,但没有成功,当我四处搜索时,我发现有几个人也遇到了这个问题以及新 OS 但没有有用的答案。

有人面临同样的问题,有什么建议吗?

非常感谢,

马克

最初我试过这个(见下面的更新):

http://cocoa-dev.apple.narkive.com/iSLaiCLR/strange-toolbar-item-error

总结:

My options are:

  • Change the max size as above, and have toolbar items potentially appear the wrong size pre-Sierra;

  • Ignore the warning and have everything appear as it should in each version;

另请注意:

I thought I'd already tried this without success, until I realized what was happening: I had edited the values, then closed the toolbar editor in IB. I've now realised that the changes were not being saved; when I reopened the toolbar editor, the old values were still showing. Running once with the toolbar editor open seems to have made the changes 'take". (And yes, I tried cleaning at various stages.)

2016 年 12 月更新 ------

出于某种原因,minSize 仍然不时随机变化。最后,我将所有这些 NSToolBarItems 链接到以下 class,这已为我修复它:

    import Cocoa

    class ToolbarItemAvoidMinWarningIssue: NSToolbarItem {

    var widthT: CGFloat = 60
    var heightT: CGFloat = 27

    override var minSize: NSSize{
        get {
            return NSSize(width: widthT, height: heightT)
        }
        set {
            widthT = newValue.width
        }
    }

}

对我来说,如上所述更改 NSToolbaritem 的 maximum 大小没有用。 但是改变它的 minimum 大小就可以了。警告消息现已消失。

我实际上在这个问题上浪费了更多的时间,但摆脱警告是我的工作之一。对我来说,无论我将工具栏按钮 min/max 的大小更改为什么,它都会抱怨大小不正确一两个像素。我不小心偶然发现了以下解决方法。我的按钮使用 'Regular' 控件大小。在 IB 中,我将每个 NSButton 从 Regular 更改为 Small,从 Small 更改为 Mini,然后将 Mini 更改回 Regular。然后我将每个 NSToolbarItem 的最小高度向上调整为比最大值小两个。我不确定在这个过程中究竟发生了什么。可能有一个更简单的解决方案,但我现在已经提交了故事板,但我仍然屏住呼吸,警告不会再出现了!

无法在界面生成器中解决此问题。然而,在 NSToolbarItem 的子类中覆盖 minSize 解决了这个问题。

- (NSSize)minSize
{
    if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_12) {
        /* Overriding this getter seems to be the only solution for runtime error logs like: NSToolbarItem (<APMRegularToolbarItem: 0x60e000039460>) had to adjust the size of <NSButton: 0x60f0001acce0> from {40, 25} to the expected size of {42, 27}. Make sure that this toolbar item view has a valid frame/min/max size. This is an app bug, please do not file a bug against AppKit or NSToolbar! Break on _NSToolbarAdjustedBorderedControlSizeBreakpoint
         */
        return NSMakeSize(42, 27);
    }
    else {
        return [super minSize];
    }
}

找到问题了!问题是在 Xcode 的 IB 中,minSize 字段仅 one-way 绑定到 XIB 源。如果您在 IB 中更改 NSToolbarItem minSize,它会适当地保存它。但是,如果您重新打开面板,通过重新打开 Xcode、重新打开项目或什至只是重新打开属性面板,它会再次显示默认值。因此,此时的属性面板可能会显示 W 127 H 25,即使 XIB 文件的来源(XML)显示 W 129 H 27(无论您上次尝试设置什么值)。因此 Xcode IB 的属性面板中的 minSize 字段值设置不正确。这导致令人困惑的情况,即在重新打开 NSToolbarItem 的属性面板后保存,您的更改将再次被覆盖。这似乎是一个 Xcode 错误。 @Marius 的回答解决了这个运行时,另一个解决方案是在设置 minSize 后不再打开 NSToolbarItem 属性。