本例子实现在NSSplitview控件上下拖动或左右拖动时 两个子窗口自动调整的按钮
// AppDelegate.h
// Cocoa_SplitView
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate,NSSplitViewDelegate>
@property (assign) IBOutlet NSTextView *textView;
@property (assign) IBOutlet NSSplitView *splitView;
@property (assign) IBOutlet NSTableView *tableView;
@property (assign) IBOutlet NSButton *button;
@property (assign) IBOutlet NSWindow *window;
- (IBAction)dosome:(id)sender;
@end
//
// AppDelegate.m
// Cocoa_SplitView
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize textView = _textView;
@synthesize splitView = _splitView;
@synthesize tableView = _tableView;
@synthesize button = _button;
- (IBAction)dosome:(id)sender
{
NSSize size = {50,50};
[[self.textView enclosingScrollView] setFrameSize:size];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (void) splitViewWillResizeSubviews:(NSNotification *)notification
{
NSLog(@"splitViewWillResizeSubviews");
}
- (void) splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
NSLog(@"resizeSubviewsWithOldSize");
}
- (BOOL) splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view
{
NSLog(@"shouldAdjustSizeOfSubview");
return YES;
}
- (void) splitViewDidResizeSubviews:(NSNotification *)notification
{
NSLog(@"splitViewDidResizeSubviews");
NSRect rect = [self.splitView frame];
NSLog(@"nssplitview %f,%f,%f,%f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
NSArray *subviews = [self.splitView subviews];
NSView *topView = [subviews objectAtIndex:0];
NSRect trect = [topView frame];
NSLog(@"top view %f,%f,%f,%f",trect.origin.x,trect.origin.y,trect.size.width,trect.size.height);
[[self.textView enclosingScrollView] setFrame:trect];
NSView *bottomView = [subviews objectAtIndex:1];
NSRect brect = [bottomView frame];
NSPoint point = {0,0};
NSRect lbrect = {point,brect.size};
[[self.tableView enclosingScrollView] setFrame:lbrect];
NSLog(@"bottom view %f,%f,%f,%f",brect.origin.x,brect.origin.y,brect.size.width,brect.size.height);
}
@end
注意:
1.在nssplitview控件的属性中一定不能钩选择“USE Autolayout”否则在委托处理中的逻辑将失效
2.nssplitview的委托一定要设置为appdelegate
有用资源:http://www.cocoabuilder.com/archive/cocoa/326128-nssplitview-not-resizing-subviews-if-delegate-is-used.html