Stop NSSplitView from resizing with Window?

  • Hi all,

    I've been searching around for a bit, and can't seem to find the
    answer to how I can get my NSSplitView from automatically resizing
    with the Window as I resize it. I'd like the splitview (exactly like
    Mail, or iTunes, for example) to stay in place as I resize the window.

    Thanks in advance,
    Brian Amerige
    http://extendmac.com
  • On Oct 12, 2006, at 6:39 PM, Brian Amerige wrote:

    > I've been searching around for a bit, and can't seem to find the
    > answer to how I can get my NSSplitView from automatically resizing
    > with the Window as I resize it. I'd like the splitview (exactly
    > like Mail, or iTunes, for example) to stay in place as I resize the
    > window.

    You need to add the delegate method -
    splitView:resizeSubviewsWithOldSize: to the split view's delegate,
    and pay attention to whether or not the split view is in a live
    resize. If it's in a live resize, then you need to keep the size of
    one view constant while resizing the other view.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • What Nick Zitzmann said is correct. Here is the code to make your life
    easier. This is for a horizontal orientation, that you mentioned you were
    looking for. (Just watch out that the divider thickness is set to 1, the
    regular divider thickness is 10, be sure to update that part of the code to
    fit your project.)

    /*  During live resize of the window, lock the left side, the collection
    side, and resize the tableside
        The frames have to be set to add up to the total size minus the divider
    thickness */
    - (void)splitView:(NSSplitView *)sender
    resizeSubviewsWithOldSize:(NSSize)oldSize {

        //detect if it's a window resize
        if ([self inLiveResize]) {

            //info needed
            NSRect tmpRect = [sender bounds];
            NSArray *subviews =  [sender subviews];
            NSView *collectionsSide = [subviews objectAtIndex:0];
            NSView *tableSide = [subviews objectAtIndex:1];
            float colllectionWidth = [collectionsSide bounds].size.width;

            //tableside frame: full size minus collection width and divider
            // My divider thickness is hard coded here as 1
            tmpRect.size.width = tmpRect.size.width - colllectionWidth + 1;
            tmpRect.origin.x = tmpRect.origin.x + colllectionWidth + 1;
            [tableSide setFrame:tmpRect];

            //collection frame stays the same
            tmpRect.size.width = colllectionWidth;
            tmpRect.origin.x = 0;
            [collectionsSide setFrame:tmpRect];

        }
        else
            [super adjustSubviews];
    }

    Regards,
    Conor
    http://www.bruji.com/
  • The code is a nice example, but why hard-code the divider
    thickness? Since you have to subclass NSSplitView to override its
    divider thickness anyway, -dividerThickness should always return the
    'right' value -- why hard-code it in delegate methods?

      Why not just always ask the split view for its -
    dividerThickness? :-) This will make the code you posted more easily
    reusable.

    --
    I.S.

    On Oct 13, 2006, at 4:56 AM, Conor Dearden wrote:

    > (Just watch out that the divider thickness is set to 1, the
    > regular divider thickness is 10, be sure to update that part of the
    > code to
    > fit your project.)
  • Hi all,

    I changed the method Conor Dearden gave me a bit ago, and it fixes a
    few things problems I had with it. Please note I've not tested it
    with anything other than my configuration, which is with a 1 pixel
    thickness vertical splitview.

    For one, it dynamically figures out the thickness of the divider so
    it's not hard-coded.
    For two, to allow for the scrollbar to be fully displayed properly,
    it adjusts the left view properly.
    For three, after resizing the window, if the window loses focus
    (say, clicking into another app), the splitView divider doesn't
    disappear, as it only calls "adjustSubviews" on the first launch.

    int x=0;
    - (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:
    (NSSize)oldSize
    {
    x++;
        //detect if it's a window resize
        if ([sender inLiveResize])
    {

            //info needed
            NSRect tmpRect = [sender bounds];
            NSArray *subviews =  [sender subviews];
            NSView *collectionsSide = [subviews objectAtIndex:0];
            NSView *tableSide = [subviews objectAtIndex:1];
            float colllectionWidth = [collectionsSide bounds].size.width;

            //tableside frame: full size minus collection width and divider
            // My divider thickness is hard coded here as 1
            tmpRect.size.width = tmpRect.size.width - colllectionWidth +
    ([sender dividerThickness]-[sender dividerThickness]);
            tmpRect.origin.x = tmpRect.origin.x + colllectionWidth +
    [sender dividerThickness];
            [tableSide setFrame:tmpRect];

            //collection frame stays the same
            tmpRect.size.width = colllectionWidth;
            tmpRect.origin.x = 0;
            [collectionsSide setFrame:tmpRect];
        }
    else if (x==1)
    {
      [sender adjustSubviews];
    }
    }

    Brian Amerige
    http://extendmac.com
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today