在水平 NSSplitView 的顶部窗格中重新定位 NSBox

发布于 2024-08-15 05:09:42 字数 1119 浏览 5 评论 0原文

我遇到的问题与 Cocoa 中的坐标系有关,但我真的不知道。这一切都发生在水平 NSSplitView 的顶部窗格中。

非常简单,我试图将一个 NSBox 放置在第二个 NSBox 的正下方(我将自定义视图加载到框中 - 一切正常)。顶部框的左上角位于窗格的左上角并且永远不会改变。如果顶部 NSBox 的高度缩小,我希望第二个 NSBox 的顶部向右滑动到其下方。相反,如果顶部 NSBox 的高度增加,我希望底部 NSBox 向下滑动。

该代码被调用两次。 Box 是正确的(第一次顶部框,第二次底部框)并且 v 是正确的(这是我正在加载到框中的视图 - 这工作正常,这就是导致顶部框高度发生变化的原因)。

 NSSize destBoxSize = [[box contentView] frame].size;  //the size of the box in the view to load the view into
 NSSize newViewSize = [v frame].size;  // the size of the view to be loaded 

 float deltaWidth = [horizSplitView frame].size.width - destBoxSize.width;
 float deltaHeight = newViewSize.height - destBoxSize.height;
 NSRect boxFrame = [box frame];
 boxFrame.size.height += deltaHeight;
 boxFrame.size.width += deltaWidth; 
 boxFrame.origin.y -= deltaHeight;

 NSLog(@"vc=%@ boxFrame x%f y%f h%f w%f", nibName, boxFrame.origin.x, boxFrame.origin.y, boxFrame.size.height, boxFrame.size.width);

 // Clear the box for resizing
 [box setContentView:nil]; 
 [box setContentView:v];
 [box setFrame:boxFrame];

I the issue I'm having has to do with the coordinate system in Cocoa but I really don't know. This is all happening in the top pane of a horizontal NSSplitView.

Very simply, I'm trying to position one NSBox right below a second one (I load custom views into the boxes - that all works fine). The top box's top-left corner is at the top-left corner of the pane and never changes. If the height of the top NSBox shrinks I want the top of the second NSBox to slide right up below it. Conversely, if the top NSBox's height increases I want the bottom NSBox to slide down.

This code gets called twice. Box is correct (first time top box, second time bottom box) and v is correct (this is the view I'm loading into the box - this works fine and it is what is causing the height to change in the top box).

 NSSize destBoxSize = [[box contentView] frame].size;  //the size of the box in the view to load the view into
 NSSize newViewSize = [v frame].size;  // the size of the view to be loaded 

 float deltaWidth = [horizSplitView frame].size.width - destBoxSize.width;
 float deltaHeight = newViewSize.height - destBoxSize.height;
 NSRect boxFrame = [box frame];
 boxFrame.size.height += deltaHeight;
 boxFrame.size.width += deltaWidth; 
 boxFrame.origin.y -= deltaHeight;

 NSLog(@"vc=%@ boxFrame x%f y%f h%f w%f", nibName, boxFrame.origin.x, boxFrame.origin.y, boxFrame.size.height, boxFrame.size.width);

 // Clear the box for resizing
 [box setContentView:nil]; 
 [box setContentView:v];
 [box setFrame:boxFrame];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2024-08-22 05:09:42

你想做的事情并不难,但需要一些子类化。首先,您需要对 NSSplitView 进行子类化,并重写 -(void)init 或 -(void)awakeFromNib 以添加此行:

[self setAutoresizesSubviews:YES];  //

然后您需要对两个框进行子类化并设置它们的自动调整大小掩码,可以在 -(void )init 或 - (void)viewWillMoveToSuperview:(NSView *)newSuperView.
对于第一个框,您可能需要:

[newInstance setAutoresizingMask:NSViewNotSizable];

对于第二个框,您可能需要:

[newInstance setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];

另请参阅 NSView。需要进行一些尝试才能获得正确的组合,但效果非常好。

What you want to do is not so hard, but it will need some subclassing. First of all, you need to subclass NSSplitView and either and override either -(void)init or -(void)awakeFromNib to add this line:

[self setAutoresizesSubviews:YES];  //

Then you need to subclass the two boxes and set their auto resizing masks, either in -(void)init or in - (void)viewWillMoveToSuperview:(NSView *)newSuperView.
For the first box you'll probably want:

[newInstance setAutoresizingMask:NSViewNotSizable];

For the second bbox you'll probably want:

[newInstance setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];

See also NSView. It takes a bit of experimenting to get the right combination, but then it works quite nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文