我正在尝试找到一种以编程方式调整大小的方法NSSplitViewItems
。
假设NSWindow
帧大小为 500x500px。
masterViewItem
宽度为 100px,而subViewItem
宽度为 400px。
时FOO
,我想masterViewItem
折叠到 0px(向左),同时subViewItem
展开以填充整个窗口(500px)。
时BAR
,我想masterViewItem
展开回 100px,而subViewItem
折叠回原来的 400px。
- (IBAction) onTapOnSomeButton:(NSButton *)sender {
NSWindow *window = [[NSApplication sharedApplication] mainWindow];
NSSplitViewController *splitViewController = (NSSplitViewController *)[window contentViewController];
NSSplitViewItem *masterViewItem =[splitViewController.splitViewItems firstObject];
NSSplitViewItem *subViewItem = [splitViewController.splitViewItems lastObject];
if (FOO) {
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// Expand subViewItem
} completionHandler:^{
}];
} else if (BAR) {
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// Expand masterViewItem while shrinking subViewItem
} completionHandler:^{
}];
}
}
我能够为每个项目设置动画,使其看起来折叠/展开,即
subViewItem.viewController.view.animator.frame = CGRectMake(0, 0, window.frame.size.width, subViewItem.viewController.view.frame.size.height);
但是分隔线不会移动,使两个项目只是单独移动,并且看起来整个屏幕都不会折叠/展开。
我将如何实现这一点?提前致谢。
更新
所以我做了更多的研究,发现了这个:
所以我做了这样的代码:
- (IBAction) onTapOnSomeButton:(NSButton *)sender {
NSWindow *window = [[NSApplication sharedApplication] mainWindow];
NSSplitViewController *splitViewController = (NSSplitViewController *)[window contentViewController];
NSSplitViewItem *masterViewItem =[splitViewController.splitViewItems firstObject];
NSSplitViewItem *subViewItem = [splitViewController.splitViewItems lastObject];
if (FOO) {
NSMutableDictionary *collapseMainAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
[collapseMainAnimationDict setObject: subView forKey:NSViewAnimationTargetKey];
NSRect newRightSubViewFrame = subView.frame;
newRightSubViewFrame.size.width = splitViewController.splitView.frame.size.width;
[collapseMainAnimationDict setObject:[NSValue valueWithRect:newRightSubViewFrame] forKey:NSViewAnimationEndFrameKey];
NSMutableDictionary *collapseInspectorAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
[collapseInspectorAnimationDict setObject: masterView forKey:NSViewAnimationTargetKey];
NSRect newLeftSubViewFrame = masterView.frame;
newLeftSubViewFrame.size.width = 0.0f;
newLeftSubViewFrame.origin.x = splitViewController.splitView.frame.size.width;
[collapseInspectorAnimationDict setObject:[NSValue valueWithRect:newLeftSubViewFrame] forKey:NSViewAnimationEndFrameKey];
NSViewAnimation *collapseAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects: collapseMainAnimationDict, collapseInspectorAnimationDict, nil]];
[collapseAnimation setDuration:0.3f];
[collapseAnimation startAnimation];
[splitViewController.splitView adjustSubviews];
[splitViewController.splitView setNeedsDisplay: YES];
} else if (BAR) {
// Not yet implemented
}
}
但是没有效果。有什么建议么?