2011-12-21 64 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

相關問題