问题
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!
回答1:
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.
回答2:
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 }
}
回答3:
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.
回答4:
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.
回答5:
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)
}
来源:https://stackoverflow.com/questions/1957498/how-to-hide-a-divider-of-nssplitview