NSToolbar insertItemWithItemIdentifier:atIndex:调用导致“无法同时满足约束”异常

时间:2014-04-25 15:39:01

标签: cocoa autolayout nstoolbar nstoolbaritem

在使用自动布局的窗口中,以下代码会导致自动布局"无法同时满足约束"例外:

  [self.toolbar insertItemWithItemIdentifier:@"MyID" atIndex:2];

异常详情如下:

2014-04-25 17:31:41.354 AppName[5100:303] Unable to simultaneously satisfy constraints:
(
    "<NSAutoresizingMaskLayoutConstraint:0x108adef10 h=--& v=--& V:[NSToolbarItemViewer:0x101d3ef80(5)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x108ade420 h=--& v=-&- V:|-(10)-[_NSToolbarSpace:0x101d37ae0]   (Names: '|':NSToolbarItemViewer:0x101d3ef80 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x108ade3c0 h=--& v=-&- V:[_NSToolbarSpace:0x101d37ae0]-(11)-|   (Names: '|':NSToolbarItemViewer:0x101d3ef80 )>"
)

Will attempt to recover by breaking constraint 
<NSAutoresizingMaskLayoutConstraint:0x108ade420 h=--& v=-&- V:|-(10)-[_NSToolbarSpace:0x101d37ae0]   (Names: '|':NSToolbarItemViewer:0x101d3ef80 )>

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens.  And/or, break on objc_exception_throw to catch this in the debugger.

工具栏项目插入灵活空间项目后面。工具栏的部分配置在IB中,但我需要动态填充多个按钮。

由于IB中的工具栏中没有任何手动指定的约束,因此我无从何处离开...

只要将工具栏项插入工具栏,一切都可以正常工作。

1 个答案:

答案 0 :(得分:3)

FWIW - 删除(预配置的)工具栏中的最后一个项目时,例外情况消失了,该工具栏恰好是灵活空间项目

现在我只需想办法让所有工具栏项都居中......

(这是在10.8 / 10.9之下)