Hiding Dividers in NSSplitView

混江龙づ霸主 提交于 2020-02-16 06:51:31

问题


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.

  1. Subclass NSSplitView and return 0 from dividerThickness

So your new split view won't display dividers at all now, but you can add them manually where you want them

  1. Add NSBox or your custom divider views where you want your dividers to show up in your split view subviews, preferrably at the top of the subview.

  2. Override the split view delegate method splitView(:additionalEffectiveRectOfDividerAt:) and manually return rects that match your custom NSBox dividers

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!