在NSSplitView中隐藏分隔线 [英] Hiding Dividers in NSSplitView

查看:138
本文介绍了在NSSplitView中隐藏分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 NSSplitView 不允许隐藏其分隔符(委托方法仅允许隐藏位于拆分视图边缘的分隔符),我选择将 NSSplitView 子类化并覆盖其绘制方法,以防止绘制特定的分隔线.

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.

但是,只要我覆盖 draw(rect:) drawDivider(in:) NSSplitView 便不再对其分隔符进行动画处理如果我像这样折叠物品

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

如果我直接调用 super 而没有添加自己的绘图代码,甚至会发生这种情况

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.

基本上我想要实现的是将拆分视图项隐藏在其分隔符旁边,但这显然要求太多 NSSplitView 而又没有完全重新实现它.

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?

推荐答案

好吧,我采用了另一种完全不同的方法,并找到了一种使其工作的方法.因此,如果您要完全自定义分隔线,这就是您的操作方式.

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. 子类 NSSplitView 并从 dividerThickness
  2. 返回 0

因此您的新拆分视图现在根本不会显示分隔线,但是您可以在所需的位置手动添加分隔线

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

  1. 添加 NSBox 或自定义分隔线视图,以使分隔线显示在拆分视图子视图中,最好显示在子视图的顶部.

  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.

重写拆分视图委托方法 splitView(:additionalEffectiveRectOfDividerAt:)并手动返回与您的自定义 NSBox 分隔符

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

您可能需要在 NSView 坐标之间进行 convert(:from:),以获取有效的矩形,但这确实可行!委托人可能看起来像这样

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!

这篇关于在NSSplitView中隐藏分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆