3

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

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

但它没有工作,为什么?这个方法怎么用?非常感谢你!

4

5 回答 5

10

除了上面@carmin的注释,覆盖 NSSplitViewdividerThickness属性是唯一对我有用的东西(特别是从 splitView:effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView 委托方法返回 NSRectZero -如此处详述 - 没有工作并导致浮动分隔线与视图本身脱节)。

这是 Swift 中的代码:

override var dividerThickness:CGFloat
{
    get { return 0.0 }
}
于 2015-03-14T13:17:31.097 回答
5

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

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

于 2009-12-24T11:17:25.903 回答
3

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

于 2013-11-03T01:16:04.740 回答
2

以下是在不涉及子类化的 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。

于 2016-02-29T16:32:34.233 回答
1

为了后代,使用 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)
}
于 2015-11-06T16:57:05.010 回答