如何在Cocoa中显示/隐藏第二个垂直分割视图?

时间:2011-12-21 12:03:39

标签: objective-c xcode cocoa

让我介绍一下。我是一名有c,c ++,java经验的狂热程序员(非专业人士),现在从MacOsx上的Objective-C和Cocoa开始。

在我的第一个程序中,我想创建两个垂直分割视图,左边一个(主要)始终打开,右边一个按钮按下显示/隐藏(它的用途是调试输出)。

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

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

提前感谢您的帮助。

路易斯

2 个答案:

答案 0 :(得分:1)

承诺,这是我最终解决问题的方法。

  • 目标:我想要两个垂直分割视图:
    • 按钮显示/隐藏右视图,并相应调整窗口大小。
    • 左一个(主要)永远在线,NON可调整宽度(可以调整高度)
    • 右边一个显示/隐藏,可以调整宽度/高度,必须始终具有最小宽度。
    • 当隐藏右边时,主窗口最小宽度/高度等于左视图

我创建了一个带有2个自定义视图的NSSplitView(垂直),在Interface Builder中有足够的自动调整限制('spring'/'s​​truts')。然后我做了以下事情:

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的想法,并希望上面的示例可以帮助其他人。

路易斯

答案 1 :(得分:0)

一个非常粗略的想法。使用 setPosition:ofDividerAtIndex:更改视图的宽度 setPosition:(CGFloat)位置DividerAtIndex:(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并调整它们。这样我就不必担心用户拖动拆分器了。