objective-c 如何以编程方式创建 Cocoa 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a Cocoa window programmatically?
提问by Steve McLeod
My Cocoa app needs some small dynamically generated windows. How can I programmatically create Cocoa windows at runtime?
我的 Cocoa 应用程序需要一些动态生成的小窗口。如何在运行时以编程方式创建 Cocoa 窗口?
This is my non-working attempt so far. I see no result whatsoever.
到目前为止,这是我的非工作尝试。我看不到任何结果。
NSRect frame = NSMakeRect(0, 0, 200, 200);
NSUInteger styleMask = NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window = [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreRetained defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window display];
回答by Jason Coco
The problem is that you don't want to call display, you want to call either makeKeyAndOrderFrontor orderFrontdepending on whether or not you want the window to become the key window. You should also probably use NSBackingStoreBuffered.
问题是你不想调用display,你想调用makeKeyAndOrderFront或者orderFront取决于你是否希望窗口成为关键窗口。您可能还应该使用NSBackingStoreBuffered.
This code will create your borderless, blue window at the bottom left of the screen:
此代码将在屏幕左下角创建无边框蓝色窗口:
NSRect frame = NSMakeRect(0, 0, 200, 200);
NSWindow* window = [[[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];
//Don't forget to assign window to a strong/retaining property!
//Under ARC, not doing so will cause it to disappear immediately;
// without ARC, the window will be leaked.
You can make the sender for makeKeyAndOrderFrontor orderFrontwhatever is appropriate for your situation.
您可以让发件人适合您的情况makeKeyAndOrderFront或orderFront任何适合您的情况。
回答by johndpope
A side note, if you want to programatically instantiate the application without a main nib, in the main.m file / you can instantiate the AppDelegate as below. Then in your apps Supporting Files / YourApp.plist Main nib base file / MainWindow.xibdelete this entry. Then use Jason Coco's approach to attach the window in your AppDelegates init method.
旁注,如果你想在没有主笔尖的情况下以编程方式实例化应用程序,在 main.m 文件中/你可以实例化 AppDelegate,如下所示。然后在您的应用程序 Supporting Files / YourApp.plist Main nib base file / MainWindow.xib 中删除此条目。然后使用 Jason Coco 的方法在 AppDelegates init 方法中附加窗口。
#import "AppDelegate.h":
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[NSApp setDelegate:appDelegate];
[NSApp run];
[pool release];
return 0;
}
回答by Rich Catalano
Try
尝试
[window makeKeyAndOrderFront:self];
instead of
代替
[window display];
Is that what you're aiming for?
这就是你的目标吗?
回答by Steve McLeod
This is what I've come up with myself:
这是我自己想出来的:
NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask = NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window = [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];
This displays a blue window. I hope this is the optimal approach.
这将显示一个蓝色窗口。我希望这是最佳方法。
回答by Jeff Younker
Translating the top rated answer to modern swift (5) gives you something akin to this:
将评分最高的答案翻译成现代 swift (5) 会给你类似的东西:
var mainWindow: NSWindow!
...
mainWindow = NSWindow(
contentRect: NSMakeRect(0, 0, 200, 200),
styleMask: [.titled, .resizable, .miniaturizable, .closable],
backing: .buffered,
defer: false)
mainWindow.backgroundColor = .blue
mainWindow.makeKeyAndOrderFront(mainWindow)

