1. 创建一个window
NSRect frame = CGRectMake(0, 0, 200, 200);
NSUInteger style = NSTitledWindowMask | NSClosableWindowMask |NSMiniaturizableWindowMask | NSResizableWindowMask;
NSWindow *window = [NSWindow alloc]initWithContentRect:frame styleMask:style backing:NSBackingStoreBuffered defer:YES];
window.title = @"New Create Window";
[window makeKeyAndOrderFront:self];
[window center];
2. 关闭窗口退出程序
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
return YES;
}
- (void)windowWillClose:(NSNotification *)notification {
NSWindow *window = notification.object;
if(window == self.window) {
[NSApp terminate:self];
}
}
3. 设置window的image和title

- (void)setWindowIcon {
[self.window setRepresentedURL:[NSURL URLWithString:@"WindowTitle"]];
[self.window setTitle:@"我是title"];
NSImage *image = [NSImage imageNamed:@"windowIcon"];
[[self.window standardWindowButton:NSWindowDocumentIconButton] setImage:image];
}
4. 最小化窗口
[self.window miniaturize:sender];
5. 点击背景可以拖动窗口
[self.window setMovableByWindowBackground:YES]
6. 隐藏titlebar
self.window.titleVisibility = NSWindowTitleHidden;
self.window.titlebarAppearsTransparent = YES;
self.window.styleMask = self.window.styleMask | NSWindowStyleMaskFullSizeContentView;
参考文献
- NSWindow
- 窗口对象