NSSplitView 三个区域拖动联动调整相关控件大小

在上篇对两级联动nssplit的基础上面升级一个版本,使用两个nssplitview(内嵌方式)

其中最上方和最下方两个区域的大小随拉动线动态调整较小代码即可实现,但中间相对复杂,直接看代码。

源码下载:http://download.csdn.net/detail/bjbs_270/6455529


//
//  AppDelegate.h
//  Cocoa_splitview_v2

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate,NSSplitViewDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet NSSplitView *splitOuter;
@property (assign) IBOutlet NSSplitView *splitInner;
@property (assign) IBOutlet NSTableView *leftTableView;
@property (assign) IBOutlet NSTableView *rightTableView;

@property (assign) IBOutlet NSTextView *textViewConsole;
@property (assign) IBOutlet NSTableView *tableViewProgress;

@end

//
//  AppDelegate.m
//  Cocoa_splitview_v2

#import "AppDelegate.h"

@implementation AppDelegate

- (void) awakeFromNib
{
    NSSize leftTableViewSize = [self.leftTableView frame].size;
    NSLog(@"---left table view: w:%f,h:%f",leftTableViewSize.width,leftTableViewSize.height);
    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}


- (void) splitViewDidResizeSubviews:(NSNotification *)notification
{
    
    NSSplitView *split = [notification object];
    NSString *splitName = [split identifier];
    
//    NSLog(@"split name is %@",[split identifier]);
    
    NSArray *allviews = [split subviews];
    
    if ([splitName isEqualToString:@"split_outer"]) {
        //1.
        NSView *topView = [allviews objectAtIndex:0];
        [[self.textViewConsole enclosingScrollView] setFrame:[topView frame]];
        
        NSView *bottomView = [allviews objectAtIndex:2];
        NSSize bottomSize = [bottomView frame].size;
        NSRect bottomRect = {0,0,bottomSize.width,bottomSize.height};
        [[self.tableViewProgress enclosingScrollView] setFrame:bottomRect];
        
        //2.
        NSView *middleView = [allviews objectAtIndex:1];
        NSRect middleRect = [middleView frame];
        
        NSLog(@"x:%f,y:%f,w:%f,h:%f",middleRect.origin.x,middleRect.origin.y,middleRect.size.width,middleRect.size.height);
        
        //3. 修改中间区域下下拉动时对应其它控件的大小
        NSPoint point = NSMakePoint(0, 0);
        [self.splitInner setFrameOrigin:point];
        [self.splitInner setFrameSize:middleRect.size];
        
        NSArray *innerSubViews = [self.splitInner subviews];
        
        NSView *leftTableViewOuterView = [innerSubViews objectAtIndex:0];
        NSSize leftTableViewOuterViewSize = [leftTableViewOuterView frame].size;
        [[self.leftTableView enclosingScrollView] setFrameSize:leftTableViewOuterViewSize];
        [[self.leftTableView enclosingScrollView] setFrameOrigin:point];
        
        NSView *rightTableViewOuterView = [innerSubViews objectAtIndex:1];
        NSSize rightTableViewOuterViewSize = [rightTableViewOuterView frame].size;
        [[self.rightTableView enclosingScrollView] setFrameSize:rightTableViewOuterViewSize];
        [[self.rightTableView enclosingScrollView] setFrameOrigin:point];
        
    } else if([splitName isEqualToString:@"split_inner"]){
        NSLog(@"xxxxxxx");//TODO
    }
}

@end


你可能感兴趣的:(cocoa,NSTextView,NSTableView,NSSplitview)