如何隐藏 nssplitview 的分隔线?

发布于 2024-08-16 11:13:57 字数 259 浏览 4 评论 0原文

现在,我想在应用程序运行时根据我的情况隐藏或显示分隔线。使用了这个委托方法:

- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
   if (A) 
       return YES;
   else 
       return NO;
}

但它不起作用,为什么?如何使用这个方法呢?非常感谢!

Now I want to hide or show with my condition a divider when my app run. used this delegate method:

- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
   if (A) 
       return YES;
   else 
       return NO;
}

but it didn't work , why? How to used this method? Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

病毒体 2024-08-23 11:13:57

根据@carmin的注释,上面,覆盖 NSSplitView dividerThickness 属性是唯一对我有用的东西(具体来说,从 splitView: effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView 委托方法返回 NSRectZero — 如此处详细说明 - 不起作用并导致浮动分隔线与视图本身脱节)。

这是 Swift 中的代码:

override var dividerThickness:CGFloat
{
    get { return 0.0 }
}

Further to @carmin’s note, above, overriding the NSSplitView dividerThickness property is the only thing that worked for me (specifically, returning NSRectZero from the splitView:effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView delegate method — as detailed here – didn’t work and resulted in floating dividers disjointed from the views themselves).

Here’s the code in Swift:

override var dividerThickness:CGFloat
{
    get { return 0.0 }
}
花落人断肠 2024-08-23 11:13:57

分割视图将该消息发送给其委托,以询问委托是否应该隐藏该分隔线。所以, 成为代理人,并回答分割视图的问题。

请务必查看 文档。该消息可能无法实现您想要的效果。该文档列出了您可以通过响应该消息执行的所有操作。

The split view sends that message to its delegate, to ask the delegate whether it should hide that divider. So, be the delegate, and answer the split view's question.

Be sure to check out the documentation. It's possible that that message won't accomplish what you want it to. The documentation lists everything you can do by responding to that message.

南街九尾狐 2024-08-23 11:13:57

您可以重载 NSSplitView-dividerThickness 并返回 0 以隐藏所有分隔线。您可以重载 NSSplitView-drawDividerInRect: 对分隔线进行单独控制(选择是否允许 super 绘制分隔线)。即使子视图可见,这些选择也有效。

You can overload NSSplitView-dividerThickness and return 0 to hide all of the dividers. You can overload NSSplitView-drawDividerInRect: to have individual control over the dividers (choosing to allow super to draw the divider or not). These choices work even when the subviews are visible.

后来的我们 2024-08-23 11:13:57

以下是如何在 Obj-C 中执行此操作,不涉及子类化。确保您已连接 IB 中的 SplitView 委托。

然后在您的委托类中:

 -(NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect ofDividerAtIndex:(NSInteger)dividerIndex 
{

    if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50) //closed or almost closed
    {

    return NSZeroRect;

    }

    return proposedEffectiveRect;

}



- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex 
{

    if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50)
   {

    return YES;
   }

    return NO;
}

这将在分割视图关闭时隐藏分隔线,但在打开时显示分隔线。

如果您不希望他们即使在打开时也能够拖动它,只需删除第一个方法中的所有代码并仅返回 NSZeroRect 即可。在第二种方法中做同样的事情,只返回 YES。

Here's how to do it in Obj-C that doesn't involve subclassing. Make sure that you've got the SplitView delegate in IB connected.

Then in your delegate class:

 -(NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect ofDividerAtIndex:(NSInteger)dividerIndex 
{

    if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50) //closed or almost closed
    {

    return NSZeroRect;

    }

    return proposedEffectiveRect;

}



- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex 
{

    if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50)
   {

    return YES;
   }

    return NO;
}

This will hide the divider when the split view is closed, but show it when it is open.

If you don't want them to be able to drag it even when its open, just cut out all the code in the first method and return only NSZeroRect. Do the same in the second method and only return YES.

为了子孙后代,使用 Swift 时,您可以调用委托函数 splitView(_: effectiveRect:forDrawnRect:ofDividerAtIndex:) 并让它返回一个空的 NSRect

override func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {

    if dividerIndex == 1 {
        return NSRect()
    }
    return super.splitView(splitView, effectiveRect: proposedEffectiveRect, forDrawnRect: drawnRect, ofDividerAt: dividerIndex)
}

For the sake of posterity, working with Swift you can call the delegate function splitView(_:effectiveRect:forDrawnRect:ofDividerAtIndex:) and just have it return an empty NSRect

override func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {

    if dividerIndex == 1 {
        return NSRect()
    }
    return super.splitView(splitView, effectiveRect: proposedEffectiveRect, forDrawnRect: drawnRect, ofDividerAt: dividerIndex)
}
孤千羽 2024-08-23 11:13:57

在 NSSplitView 的自定义类中使用此类:

class customSplitView: NSSplitView {
  override var dividerThickness: CGFloat {
    return 0
  }
}

对我来说它有效!

Use this class in Custom class of NSSplitView:

class customSplitView: NSSplitView {
  override var dividerThickness: CGFloat {
    return 0
  }
}

For me it worked!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文