Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • NSSplitView delegate for priority based resizing

    • 0
    • 1
    • 2
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.04k
    Comment on it

    The default resizing mechanism in NSSplitView is proportional resizing in which if the NSSplitView changes size, each column resizes by an equal percent. But it is not successful in case where the columns in a split view are used to separate a side panels from a main view area (for example the "source list" in iTunes or the file tree in Xcode). Following is a delegate method that configures a split view for this side panel and main view behavior i.e resizing the views in a split view based on a priority list.

    - (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
    {
        NSArray *subviews = [sender subviews];
        NSInteger subviewsCount = [subviews count];
    
        BOOL isVertical = [sender isVertical];
    
        CGFloat delta = [sender isVertical] ?
        (sender.bounds.size.width - oldSize.width) :
        (sender.bounds.size.height - oldSize.height);
    
        NSInteger viewCountCheck = 0;
    
        for (NSNumber *priorityIndex in
             [[viewIndicesByPriority allKeys] sortedArrayUsingSelector:@selector(compare:)])
        {
            NSNumber *viewIndex = [viewIndicesByPriority objectForKey:priorityIndex];
            NSInteger viewIndexValue = [viewIndex integerValue]; 
            if (viewIndexValue >= subviewsCount)
            {
                continue;
            }
            NSView *view = [subviews objectAtIndex:viewIndexValue];
            NSSize frameSize = [view frame].size;
            NSNumber *minLength = [lengthsByViewIndex objectForKey:viewIndex];
            CGFloat minLengthValue = [minLength doubleValue];
            if (isVertical)
            {
                frameSize.height = sender.bounds.size.height;
                if (delta > 0 ||
                    frameSize.width + delta >= minLengthValue)
                {
                    NSLog(@"ifdelta>0 %f",frameSize.width);
                    frameSize.width += delta;
                    delta = 0;
                }
                else if (delta < 0)
                {
                    delta += frameSize.width - minLengthValue;
                    frameSize.width = minLengthValue;
                }
            }
            else
            {
                frameSize.width = sender.bounds.size.width;
                if (delta > 0 ||
                    frameSize.height + delta >= minLengthValue)
                {
                    frameSize.height += delta;
                    delta = 0;
                }
                else if (delta < 0)
                {
                    delta += frameSize.height - minLengthValue;
                    frameSize.height = minLengthValue;
                }
            }
            [view setFrameSize:frameSize];
            viewCountCheck++;
    }
        NSAssert1(viewCountCheck == [subviews count],
                  @"Number of valid views in priority list is less than the subview count"
                  @" of split view %p.",
                  sender);
        NSAssert3(fabs(delta) < 0.5,
                  @"Split view %p resized smaller than minimum %@ of %f",
                  sender,
                  isVertical ? @"width" : @"height",
                  sender.frame.size.width - delta);
    
        CGFloat offset = 0;
        CGFloat dividerThickness = [sender dividerThickness];
        for (NSView *subview in subviews)
        {
            NSLog(@"subview in subviews");
            NSRect viewFrame = subview.frame;
            NSPoint viewOrigin = viewFrame.origin;
            viewOrigin.x = offset;
            [subview setFrameOrigin:viewOrigin];
            offset += viewFrame.size.width + dividerThickness;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: