OC之窗口的基本属性
Posted sundaymac
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC之窗口的基本属性相关的知识,希望对你有一定的参考价值。
2017年11月27日 11:01:16 Amazing_anna 阅读数:886
代码示例
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];”
styleMask:窗口风格
enum {
NSBorderlessWindowMask = 0, //没有顶部titlebar边框
NSTitledWindowMask = 1 << 0, //有顶部titlebar边框
NSClosableWindowMask = 1 << 1, //带有关闭按钮
NSMiniaturizableWindowMask = 1 << 2, //带有最小化按钮
NSResizableWindowMask = 1 << 3, //恢复按钮
NSTexturedBackgroundWindowMask = 1 << 8 //带纹理背景的window
};
backing:窗口绘制的缓存模式
enum {
NSBackingStoreRetained = 0, //兼容老系统参数,基本很少用到
NSBackingStoreNonretained = 1, //不缓存直接绘制
NSBackingStoreBuffered = 2 //缓存绘制
};
窗口通知
NSWindowDidBecomeKeyNotification: 窗口成为keyWindow
NSWindowDidBecomeMainNotification:窗口成为mainWindow
NSWindowDidMoveNotification: 窗口移动
NSWindowDidResignKeyNotification: 窗口不再是keyWindow
NSWindowDidResignMainNotification:窗口不再是mainWindow
NSWindowDidResizeNotification: 窗口大小改变
NSWindowWillCloseNotification: 关闭窗口
NSWindow 中的 contentView
关闭窗口时退出程序
方法一:
- (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]; //或这句也行
}
}
设置Window的image和title
- (void)setWindowIcon {
[self.window setRepresentedURL:[NSURL URLWithString:@"WindowTitle"]];
[self.window setTitle:@"SQLiteApp"];
NSImage *image = [NSImage imageNamed:@"windowIcon"];
[[self.window standardWindowButton:NSWindowDocumentIconButton] setImage:image];
}
**关闭窗口**
[self.window miniaturize:sender];
以上是关于OC之窗口的基本属性的主要内容,如果未能解决你的问题,请参考以下文章