3

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!

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
jin
  • 2,145
  • 5
  • 27
  • 44
  • 4
    splitView:shouldHideDividorAtIndex: doesNot hide a divider for a view which isNot collapsed. – carmin Nov 03 '13 at 01:05
  • It also doesn't hide dividers for collapsed views that are not on the edge of the split view. So this delegate method is basically useless. –  Feb 11 '20 at 09:04
  • Have a look at my answer https://stackoverflow.com/questions/60165351/hiding-dividers-in-nssplitview –  Feb 11 '20 at 10:02
  • Have a look here how to properly hide dividers in the middle of a split view https://stackoverflow.com/questions/60165351/hiding-dividers-in-nssplitview –  Feb 11 '20 at 10:04

6 Answers6

10

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 }
}
Community
  • 1
  • 1
Aral Balkan
  • 5,981
  • 3
  • 21
  • 24
5

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • You need to set the delegate of the split view to an instance of your class, and that instance needs to respond to the appropriate message (see the documentation I linked to) with the correct value. – Peter Hosey Jan 21 '10 at 21:56
  • Looks like both the delegate and `dividerThickness` are required. The delegate seems to enable/disable interaction with the divider. If you skip implementing the delegate, the use can still interact with an invisible divider. – Austin Apr 19 '18 at 17:48
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.

carmin
  • 421
  • 3
  • 5
2

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.

applehelpwriter
  • 488
  • 3
  • 8
1

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)
}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
0

Use this class in Custom class of NSSplitView:

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

For me it worked!

dbc
  • 104,963
  • 20
  • 228
  • 340
  • How does this differ from [this answer](https://stackoverflow.com/a/29049380/3744182) by [Aral Balkan](https://stackoverflow.com/users/253485/aral-balkan), which states *overriding the NSSplitView `dividerThickness` property is the only thing that worked for me* as well as [this answer](https://stackoverflow.com/a/19748828/3744182) by [carmin](https://stackoverflow.com/users/1673606/carmin) which states, *You can overload NSSplitView-dividerThickness and return 0 to hide all of the dividers*? – dbc Dec 26 '22 at 16:14
  • Yes bro! For my design, they mentioned not to show the division part, it should be hidden. And my answer is similar to many previous answers, i just posted with exact code. No BIg Deal! – Gokul S Dec 27 '22 at 10:28