在水平 NSSplitView 的顶部窗格中重新定位 NSBox
我遇到的问题与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想做的事情并不难,但需要一些子类化。首先,您需要对 NSSplitView 进行子类化,并重写 -(void)init 或 -(void)awakeFromNib 以添加此行:
然后您需要对两个框进行子类化并设置它们的自动调整大小掩码,可以在 -(void )init 或 - (void)viewWillMoveToSuperview:(NSView *)newSuperView.
对于第一个框,您可能需要:
对于第二个框,您可能需要:
另请参阅 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:
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:
For the second bbox you'll probably want:
See also NSView. It takes a bit of experimenting to get the right combination, but then it works quite nicely.