2011-12-21 59 views
2

让我介绍一下。我是一名发烧友程序员(不是专业人员),拥有c,C++,java经验,现在开始在MacOsx上使用Objective-C和Cocoa。如何在Cocoa中显示/隐藏第二个垂直分割视图?

在我的第一个程序中,我想要创建两个垂直分割的视图,让左边的一个(main)总是打开并且右边的按钮按下来显示/隐藏(其用途将用于调试输出) 。

我已经看到了我想在Xcode 4.2下我们可以隐藏/显示导航/调试/实用程序。我正在寻找“公用事业”行为,这正是我想要的。该垂直视图的用法是从我的程序输出“调试”文本,我正在考虑在NSScrollView中使用NSTextView来模拟“控制台”。我知道我可以使用Xcode的Terminal或Debug视图,这就是现在的工作。我需要这个只是为了学习如何做,并改善我的计划。

我google了很多,并阅读了类似的请求,但我找不到如何做到这一点。

在此先感谢您的帮助。

Luis

回答

0

一个非常粗略的想法。改变视图的宽度随着setPosition两种:ofDividerAtIndex: setPosition两种:(CGFloat的)位置ofDividerAtIndex:(NSInteger的)dividerIndex

声明一个CGFloat的splitterlength

并把它放在applicationDidFinishLaunching中。

splitterlength = splitView.bounds.size.width; 
[splitView setPosition:splitterlength ofDividerAtIndex:0]; 

然后用这个动作

 - (IBAction)moveSplitter:(id)sender { 

    NSArray *splitterViews =splitView.subviews; 
    CGFloat splitterCheckLength =[[splitterViews objectAtIndex:0]bounds].size.width; 
    CGFloat openSplitter=splitterlength/2; 

    if (splitterCheckLength ==openSplitter) { 
     [splitView setPosition:splitterlength ofDividerAtIndex:0]; 
    }else { 
     [splitView setPosition:openSplitter ofDividerAtIndex:0]; 
    } 




} 

使用你什么都想要的长度。

虽然这样说。我会使用正常的customViews并进行调整。这样我就不用担心用户拖动分离器了。

+0

非常感谢@markhunte。我测试过它,它工作得很好。也给了我如何解决这个问题的总体思路。在这里找到另一个有趣的话题[链接](http://stackoverflow.com/questions/925020/how-to-expand-and-collapse-parts-of-nssplitview-programatically/1103242#1103242)。我将在这方面进行更多研究,并在完成后发布我的最终代码。再次感谢。路易斯 – 2011-12-21 18:41:35

1

作为承诺,这是我最终做了什么来解决我的问题。

  • 目标:我想要两个垂直分裂的观点:
    • 按钮显示/隐藏右视图和窗口相应调整。
    • 左一个(主)永远在线和在宽度NON可调整大小(它可以在高度调整大小)
    • 右一个显示/隐藏,可以在宽度/唤起注意调整大小,必须总是分钟宽度。
    • 当右键被隐藏时,主窗口最小宽度/高度等于左视图

我创建了一个NSSplitView(垂直)与2自定义视图与在界面生成足够自动调整大小的限制(“弹簧”/'支柱')。然后我做了以下:

Controller.h 
: 
@interface Controller : NSWindowController <NSSplitViewDelegate, NSWindowDelegate> { 
: 


Controller.m 
: 
// To control the Splitter (next 3 methods) 
// ======================================= 
// The splitter cannot be moved. I always return "widthViewLeft" which is "fixed static sized to the left view width" 
// I return NO to resize the left panel and YES to the right panel. 

-(CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex { 
    return (widthViewLeft);  
} 
-(CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex { 
    return (widthViewLeft); 
} 
-(BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview { 
    return subview != splitViewLeft; 
} 

// To control the Main Window resize 
// ======================================= 
// I allow to resize if the Right panel is open. 
// I restrict to a fixed size if the Right panel is closed(hidden), so I don't allow to resize. 

- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize 
{ 
    if ([[leftViewController view] isHidden ]) { 
     proposedFrameSize.width = widthViewLeft + 2; 
     proposedFrameSize.height = heightViewLeft + titleBarHeight + 2; 
    }  
    return proposedFrameSize; 
} 


// To HIDE the right panel 
// ======================================= 
// 
-(void)handleNotificationHideConsola:(NSNotification *)pNotification 
{ 
    NSRect newFrame; 
    NSSize newMinSize; 
    NSSize newMaxSize; 

    // Hide the right panel 
    [[rightViewController view] setHidden:TRUE]; 

    // Values that do not change 
    newMinSize.height = [theWindow minSize].height;  
    newMaxSize.height = [theWindow maxSize].height;  
    newFrame.origin.x = [theWindow frame].origin.x; 
    //newFrame.origin.y = [theWindow frame].origin.y; 

    // Values that change 
    newMinSize.width = widthViewLeft; 
    newMaxSize.width = widthViewLeft; 
    newFrame.size.width = widthViewLeft + 2; 
    newFrame.size.height = heightViewLeft + titleBarHeight + 2; 
    newFrame.origin.y = [theWindow frame].origin.y + ([theWindow frame].size.height - newFrame.size.height) ; 

    // Perform the change 
    [theWindow setMinSize:newMinSize]; 
    [theWindow setFrame:newFrame display:YES animate:YES]; 

} 

// To SHOW the right panel 
// ======================================= 
// 
-(void)handleNotificationShowConsola:(NSNotification *)pNotification 
{ 
    if ([[rightViewController view] isHidden]) { 
     NSRect newFrame; 
     NSSize newMinSize; 

     // Show the right panel 
     [[rightViewController view] setHidden:FALSE]; 

    // Values that do not change 
     newMinSize.height = [theWindow minSize].height;  
     newFrame.origin.x = [theWindow frame].origin.x; 
     newFrame.origin.y = [theWindow frame].origin.y ; 

     // Values that change 
     newMinSize.width = widthViewLeft + widthViewRigth;    
     newFrame.size.width = widthViewLeft + widthViewRigth;   
     newFrame.size.height = newMinSize.height + titleBarHeight; 
     newFrame.origin.y = [theWindow frame].origin.y - (newFrame.size.height - [theWindow frame].size.height); 

     // Perform the change 
     [theWindow setMinSize:newMinSize]; 
     [theWindow setFrame:newFrame display:YES animate:YES]; 
    } 
} 

再次感谢@markhunte的想法,并希望上述示例可以帮助别人。

Luis

相关问题