我来介绍一下。我是一名狂热的程序员(非专业),拥有 c、c++、java 经验,现在开始在 MacOsx 上使用 Objective-C 和 Cocoa。
在我的第一个程序中,我想创建两个垂直分割的 View ,左侧一个(主)始终打开,右侧一个按按钮显示/隐藏(其用途将用于调试输出)。
我已经在 Xcode 4.2 中看到了我想要的东西,我们可以隐藏/显示导航器/调试/实用程序。我正在寻找“实用程序”行为,这正是我想要的。该垂直 View 的用途是从我的程序中输出“调试”文本,我正在考虑使用 NSScrollView 中的 NSTextView 来模拟“控制台”。我知道我可以只使用终端或 Xcode 的调试 View ,这就是现在的工作方式。我需要这个只是为了学习如何做到这一点并改进我的程序。
我在谷歌上搜索了很多并阅读了类似的请求,但我找不到具体如何做到这一点。
预先感谢您的帮助。
路易斯
最佳答案
正如所 promise 的,这就是我最终为解决问题所做的事情。
- 目标:我想要两个垂直分割的 View :
- 用于显示/隐藏右侧 View 和窗口相应调整大小的按钮。
- 左一个(主要)始终开启且不可调整宽度(可以调整高度)
- 右侧显示/隐藏,可以调整宽度/高度大小,必须始终具有最小宽度。
- 当右侧隐藏时,主窗口最小宽度/高度等于左侧 View
我在 Interface Builder('springs'/'struts')中创建了一个带有 2 个自定义 View 的 NSSplitView(垂直),并具有足够的自动调整大小限制。然后我做了以下事情:
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 的想法,并希望上述示例对其他人有所帮助。
路易斯
关于objective-c - 如何在 Cocoa 中显示/隐藏第二个垂直分割 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8589514/