NSWindow纯代码设置

MacOS下NSWindow使用详解
本文详细介绍了在MacOS环境下如何使用NSWindow类创建、配置和操作窗口,包括窗口的创建、关闭、设置图标和标题、最小化、背景拖动、隐藏标题栏等关键功能的实现。

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

win图片标题

- (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. 窗口对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ADreamClusive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值