更改 ContenView 后 NSWindow 的位置问题
在我的应用程序中,我有一个 NSWindow 和不同的 CustomViews... 我的目标只是改变窗口的 contentView 。 我用一个按钮将主窗口放置在屏幕中央,按下按钮后执行以下代码,
[mainWindow setFrame:[mainView frame] display:TRUE animate:TRUE];
[mainWindow setContentView:mainView];
这工作正常,但调整大小后窗口位于屏幕的左下角。 我希望它保持在屏幕的中央,
当我使用时
[mainWindow center];
它会移动到左下角,调整大小后它会跳到中心..这一点都不好,它应该留在屏幕的中间
in my application i hav one NSWindow and different CustomViews...
my target is just to change the contenView of the window.
i placed the main window in the center of the screen with a button and after pressing the button the following code is executed
[mainWindow setFrame:[mainView frame] display:TRUE animate:TRUE];
[mainWindow setContentView:mainView];
this just works fine but after resizing the window is in the left bottom corner of the screen..
i want it to stay in the center of the screen
when i use
[mainWindow center];
it will move to the left bottom corner and after resizing it jumps to to center.. that not good at all it should just stay in the middle of the screen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的窗口移动到左下角的原因是
[mainWindow setFrame:[mainView框架]显示:YES animate:YES];
[
mainView框架].origin
是NSZeroPoint
,但它没有超级视图。如果您希望窗口保持居中,则需要setFrameOrigin:
。我建议使用NSMidX()
和NSMidY()
来计算距屏幕中心的偏移量。The reason your window moves to the bottom-left corner is that
[mainWindow setFrame:[mainView frame] display:YES animate:YES];
The [
mainView frame].origin
isNSZeroPoint
while it has no superview. If you want your window to stay centered, you need tosetFrameOrigin:
. I recommend usingNSMidX()
andNSMidY()
to calculate the offset from the screen's center.