问题
Since NSSplitView doesn't allow for hiding its dividers (the delegate method only allows for hiding dividers that are on the split views edge), I chose to subclass NSSplitView and override its draw methods to prevent specific dividers from drawing.
However, as soon as I override either draw(rect:) or drawDivider(in:) the NSSplitView no longer animates it's dividers if I collapse an item like so
activityItem.animator().isCollapsed = collapsed
It even happens if I call super directly without adding my own drawing code
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
The code above is enough to completely break animations.
Basically all I am trying to achieve is hiding a split view item alongside its divider, but that is apparently too much to ask of a NSSplitView without reimplementing it completely.
I'm on my last straw here. Any other method to accomplish hiding items + divider?
回答1:
Ok, I went a whole different way and found a way to make it work. So if you are trying to completely customize dividers this is how you do it.
- Subclass
NSSplitViewand return0fromdividerThickness
So your new split view won't display dividers at all now, but you can add them manually where you want them
Add
NSBoxor your custom divider views where you want your dividers to show up in your split view subviews, preferrably at the top of the subview.Override the split view delegate method
splitView(:additionalEffectiveRectOfDividerAt:)and manually return rects that match your customNSBoxdividers
You might need to convert(:from:) between NSView coordinates to get your effective rects, but it works! The delegate might look something like this
override func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
let item = splitViewItems[dividerIndex]
let itemView = item.viewController.view
let frame = view.convert(itemView.bounds, from: itemView)
let dividerFrame = CGRect(x: 0,
y: view.bounds.height - frame.minY,
width: frame.width,
height: 1)
return dividerFrame
}
There you have it. Custom dividers that also work with animations!
来源:https://stackoverflow.com/questions/60165351/hiding-dividers-in-nssplitview