在 Interface Builder 中禁用 NSSplitView 的自动布局

Disable Autolayout for NSSplitView in Interface Builder

通过在故事板中使用 NSSplitViewController,我想覆盖包含的拆分视图的行为。

通过实施一些关于滑块最大约束的 NSSplitViewDelegate 方法,我得到了这个异常:

2017-01-12 11:05:45.873814 iCache[3399:659731] [General] SplitViewController's splitView is unable to use autolayout because the SplitViewController overrides an incompatible delegate method.
2017-01-12 11:05:45.874089 iCache[3399:659731] [General] (
    0   CoreFoundation                      0x00007fffb5d9ee7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffca989cad objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffb5da3b82 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x00007fffb77edd50 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   AppKit                              0x00007fffb389ca29 -[NSSplitView _splitViewUseConstraintBasedLayout] + 355
    5   AppKit                              0x00007fffb389c894 -[NSSplitView(NSSplitViewDividerViews) _canUseDividerViewsAsSubviews] + 74
    6   AppKit                              0x00007fffb389c0f7 -[NSSplitView(NSSplitViewDividerViews) _updateDividerViews] + 36
    7   AppKit                              0x00007fffb389dd41 -[NSSplitViewController _updateSplitView:withBlock:] + 51
    8   AppKit                              0x00007fffb389dc89 -[NSSplitViewController viewDidLoad] + 144
    9   AppKit                              0x00007fffb3896283 -[NSViewController _sendViewDidLoad] + 97
    10  CoreFoundation                      0x00007fffb5d17889 -[NSSet makeObjectsPerformSelector:] + 217
    11  AppKit                              0x00007fffb3814902 -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1389
    12  AppKit                              0x00007fffb391d436 -[NSNib _instantiateNibWithExternalNameTable:options:] + 696
    13  AppKit                              0x00007fffb391d06a -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 143
    14  AppKit                              0x00007fffb403f34a -[NSStoryboard instantiateControllerWithIdentifier:] + 234
    15  AppKit                              0x00007fffb3805bb7 NSApplicationMain + 780
    16  iCache                              0x00000001000127f4 main + 84
    17  libdyld.dylib                       0x00007fffcb26d255 start + 1
)

显然这是由于用于拆分视图的自动布局所致。 有没有办法在 Interface Builder 中为此 NSSplitView 禁用自动布局?

Enable/disable 自动布局

您可以在 File Inspector 下的 Interface Builder 中禁用每个 XIB 文件的自动布局。

您应该能够通过视图 translatesAutoresizingMaskIntoConstraints.

控制每个视图的自动布局

为什么它不起作用

根据 OS X 10.11 发行说明,有些方法不能与自动布局结合使用。

请参阅 notes 中的这段摘录。

Auto Layout NSSplitView improvements

In 10.8, NSSplitView properly respects constraints applied to its subviews, such as their minimum view widths. There are also new APIs for controlling the holding priorities, which determine both the NSLayoutPriority at which a split view holds its sizes and also which views change size if the split view itself grows or shrinks.

  • (NSLayoutPriority)holdingPriorityForSubviewAtIndex:(NSInteger)subviewIndex;
  • (void)setHoldingPriority:(NSLayoutPriority)priority forSubviewAtIndex:(NSInteger)subviewIndex;

In order to take advantage of these improvements, you must NOT implement any of the following NSSplitViewDelegate methods:

  • splitView:constrainMinCoordinate:ofSubviewAt:
  • splitView:constrainMaxCoordinate:ofSubviewAt:
  • splitView:resizeSubviewsWithOldSize:
  • splitView:shouldAdjustSizeOfSubview:

These methods are incompatible with auto layout. You can typically achieve their effects and more with auto layout.