现在我想在我的应用程序运行时隐藏或显示我的条件分隔线。使用了这个委托方法:
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if (A)
return YES;
else
return NO;
}
但它没有工作,为什么?这个方法怎么用?非常感谢你!
现在我想在我的应用程序运行时隐藏或显示我的条件分隔线。使用了这个委托方法:
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if (A)
return YES;
else
return NO;
}
但它没有工作,为什么?这个方法怎么用?非常感谢你!
除了上面@carmin的注释,覆盖 NSSplitViewdividerThickness
属性是唯一对我有用的东西(特别是从 splitView:effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView 委托方法返回 NSRectZero -如此处详述 - 没有工作并导致浮动分隔线与视图本身脱节)。
这是 Swift 中的代码:
override var dividerThickness:CGFloat
{
get { return 0.0 }
}
您可以重载 NSSplitView-dividerThickness 并返回 0 以隐藏所有分隔线。您可以重载 NSSplitView-drawDividerInRect: 以单独控制分隔线(选择是否允许 super 绘制分隔线)。即使子视图可见,这些选择也有效。
以下是在不涉及子类化的 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。
为了后代,使用 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)
}