简体   繁体   English

可可窗口位置异常

[英]Cocoa window position anomaly

I have a weird problem with positioning a window on screen. 我在屏幕上定位一个窗口有一个奇怪的问题。 I want to center the window on the screen, but i don't know how to do that. 我想将窗口置于屏幕中央,但我不知道该怎么做。 Here's what i've got. 这就是我所拥有的。 The window is created from nib by the main controller: 该窗口由主控制器从笔尖创建:

IdentFormController *ftf = [[IdentFormController alloc] initWithWindowNibName:@"IdentForm"];
[[ftf window] makeKeyAndOrderFront:self];

Now the IdentFormController has awakeFromNib() method in which it tries to position the window. 现在,IdentFormController具有awakeFromNib()方法,在该方法中它尝试定位窗口。 For the sake of simplicity i've just tried to do setFrameOrigin(NSMakePoint(0, 0)). 为了简单起见,我刚尝试做setFrameOrigin(NSMakePoint(0,0))。 What happens is as follows: 会发生什么如下:

The first time i create this window, everything works as expected. 我第一次创建这个窗口时,一切都按预期工作。 But if i create it again after releasing the previous, it starts appearing at random positions. 但如果我在释放前一个之后再创建它,它会开始出现在随机位置。 Why does it do that? 为什么这样做?

So as I understand it you want to center the window on the screen? 据我所知,你想把窗口置于屏幕中心?

Well assuming NSWindow *window is your window object then there are two methods... 假设NSWindow *window是你的窗口对象,那么有两种方法......

[window center];

This is the best way to do it but it will ofset to take into account visual weight and the Dock's presence. 这是最好的方法,但它会考虑到视觉重量和Dock的存在。

If you want dead center then this would work... 如果你想要死亡中心,那么这将有效......

// Calculate the actual center
CGFloat x = (window.screen.frame.size.width - window.frame.size.width) / 2;
CGFloat y = (window.screen.frame.size.height - window.frame.size.height) / 2;

// Create a rect to send to the window
NSRect newFrame = NSMakeRect(x, y, window.frame.size.width, window.frame.size.height);

// Send message to the window to resize/relocate
[window setFrame:newFrame display:YES animate:NO];

This code is untested but it gives you a fair idea of what you need to do to get this thing working the way you want, personally I would advise you stick with Apple's code because it has been tested and is what the user would expect to see, also from a design perspective as a designer my self I don't always rely on the actual center to be where the optical center is. 这段代码是未经测试的,但是它让你很清楚你需要做些什么才能让这个东西以你想要的方式工作,我个人建议你坚持使用Apple的代码,因为它已经过测试,是用户希望看到的,从设计角度来看,作为设计师,我自己并不总是依赖于实际的中心,而是光学中心所在的位置。

You're probably running afoul of automatic window positioning. 你可能会碰到自动窗口定位。 Have you tried calling 你试过打电话吗?

[myWindowController setShouldCascadeWindows: NO];

?

First of all, it sounds like you need to check "dealloc on close" or "release on close" in the NSWindow's property inspector. 首先,听起来你需要在NSWindow的财产检查员中检查“关闭时dealloc”或“关闭时释放”。 Then the window will clean up after itself and you can remove the (risky) call to [self release] in your own code. 然后窗口将自行清理,您可以在自己的代码中删除[self release]的(风险)调用。

awakeFromNib is called after all objects from the nib have been unarchived and outlets have been connected, but that may be too early to be setting the window coordinates. 在取消归档所有来自笔尖的对象并连接出口后调用awakeFromNib ,但是设置窗口坐标可能为时尚早。 I believe Cocoa does some work to automatically position subsequent windows below and to the right of existing windows, so that new windows don't completely obscure old ones. 我相信Cocoa做了一些工作来自动定位现有窗口下方和右侧的后续窗口,以便新窗口不会完全遮盖旧窗口。 It is likely doing this AFTER you set the position in awakeFromNib, stomping on your changes. 您可以在awakeFromNib中设置位置,踩踏您的更改后执行此操作。

The best place to set your window position is probably in one of the NSWindow delegate methods ( windowWillBecomeVisible: perhaps), or possibly right before you call makeKeyAndOrderFront: . 设置窗口位置的最佳位置可能是NSWindow委托方法之一( windowWillBecomeVisible:也许),或者可能在调用makeKeyAndOrderFront:之前makeKeyAndOrderFront:

Check out if you can set the centre of your window with the centre of your screen. 检查您是否可以将窗口中心设置为屏幕中心。 And set the window position on it. 并在其上设置窗口位置。 It might work out. 它可能会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM