0

I want to use a taller (27pt) view as the divider for a NSSplitView which is vertically-stacked (splitView.isVertical = false). Is there an intended way to do this? If not, is there a common hack?

I was thinking of using a thin divider and listening for mouse drags in the custom view, and using those to programmatically change the divider's location. Is this a valid approach?

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Maybe the delegate has some methods to do this? Did you check? – Willeke Nov 16 '16 at 23:41
  • @Willeke [`NSSplitViewDelegate`](https://developer.apple.com/reference/appkit/nssplitviewdelegate) contains zero functions which return a `NSView` or similar – Ky - Nov 16 '16 at 23:53
  • How about `splitView(_:effectiveRect:forDrawnRect:ofDividerAt:)` and `splitView(_:additionalEffectiveRectOfDividerAt:)`? – Willeke Nov 17 '16 at 00:04
  • @Willeke Those are rectangles (which I tried out and only seem to affect the tracking area, not the size). I want to put a whole view in there. – Ky - Nov 17 '16 at 00:34

1 Answers1

3

An approach that doesn't require listening to mouse drags or any active updating of the view is to use auto layout:

1. Override dividerThickness to return your ideal thickness.

2. Override dividerColor to return clear if your custom view is not opaque and if you don't want the default grey showing through.

If you have a minimum deployment target of 10.11 or later:

3a. Set arrangesAllSubviews to NO on the split view and then add the custom divider view as a subview of the split view. (without setting arrangesAllSubviews to NO, adding the custom divider view would add it as a split pane)

Otherwise, if your minimum deployment target is < 10.11 (or you otherwise cannot set arrangesAllSubviews to NO):

3b. Add a your custom divider as a subview of the container of the split view, but making sure it's higher in subview/z-order

4. Add constraints to position that view where the divider would be, e.g.:

let constraints = [dividerView.topAnchor.constraint(equalTo: topPane.bottomAnchor),
                   dividerView.bottomAnchor.constraint(equalTo: topPane.bottomAnchor),
                   dividerView.leadingAnchor.constraint(equalTo: splitView.leadingAnchor),
                   dividerView.trailingAnchor.constraint(equalTo: splitView.trailingAnchor)]
NSLayoutConstraint.activate(constraints)

Auto layout will take care of updating the position of the divider view when the user drags.

Taylor
  • 3,183
  • 17
  • 18