欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NSWindow纯代码设置

程序员文章站 2022-07-14 10:09:27
...

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;
}
// 方法二:利用 NSWindowDelegate 
- (void)windowWillClose:(NSNotification *)notification { 
    NSWindow *window = notification.object; 
    if(window == self.window) { 
        [NSApp terminate:self]; 
        // [[NSApplication sharedApplication] terminate:nil]; //或这句也行 
    } 
}

3. 设置window的image和title

NSWindow纯代码设置

- (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;
// 设置contentview与titlebar融合到一起(此时设置背景颜色也将影响titlebar的颜色)
self.window.styleMask = self.window.styleMask | NSWindowStyleMaskFullSizeContentView;

参考文献

  1. NSWindow
  2. 窗口对象
相关标签: NSWindow