0

我有一个用一些表单项动态填充的 NSMatrix。现在,我可以方便地调用 [theMatrix sizeToCells] 并将其传递到要显示的面板中。

现在,我希望包含此 NSMatrix 的 NSPanel 对象调整大小以很好地环绕它。NSPanel 底部还有一个按钮,应该在 NSMatrix 下方。

我一直在尝试很多事情来获得界限和设置框架并且一直很困惑。

是否有任何标准或正确的方式来调整面板的大小?

作为一个附带问题:框架的原点是指它的左上角还是左下角?它总是一致的吗?

谢谢

4

1 回答 1

1

The origin of an NSPanel/NSWindow frame is always the bottom-left corner, it's measured from the screen origin.

Whether the origin of a view is the top left or bottom left depends on whether or not its superview is flipped. Flipped views have their bounds origin in the top left.

To do what you want, you need to get the frame size of the NSMatrix, then recalculate the layout of the panel.

Something like this (written in here, untested!):

//NSMatrix* matrix;
//NSPanel* panel;
CGFloat panelMargin = 10.0;
CGFloat matrixBottomMargin = 30.0;

[matrix sizeToCells];
NSRect matrixFrame = [matrix frame];

NSRect panelFrame = [panel frame];

NSSize newPanelSize = NSMakeSize(NSWidth(matrixFrame) + 2.0 * panelMargin, 
                NSHeight(matrixFrame) + 2.0 * panelMargin + matrixBottomMargin);

CGFloat yDelta = newPanelSize.height - NSHeight(panelFrame);

panelFrame = NSMakeRect(panelFrame.origin.x, 
                        panelFrame.origin.y - yDelta, 
                        newPanelSize.width, 
                        newPanelSize.height);
[panel setFrame:panelFrame display:YES];
于 2009-09-04T03:23:27.763 回答