Fork me on GitHub

MAC:NSWindow

目录
  1.1 隐藏标题栏
  1.2 窗口无边界
  1.3 窗口透明
  1.4 背景红色
  1.5 点击背景移动窗口
  1.6 关闭当前window
  1.7 创建新window
  1.8 实现圆角
  1.9 监听窗口大小
  1.10 设置window不可拉伸

1.1 隐藏NSWindow的标题栏
方法一:

(1)重写NSWindow子类,继承NSWindow。

- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsignedint)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag{
    
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:(NSBackingStoreType)backingType defer:YES];
    
    return self;
    
}

(2)xib ViewController 上Window 继承子类


方法二:

xib中直接取消title Bar 选中

 

1.2 设置窗口无边界
[self setStyleMask:NSBorderlessWindowMask];

 

1.3 设置窗口为透明
[self setOpaque:YES];

 

1.4 设置背景红色
[self setBackgroundColor:[NSColor redColor]];

 

1.5 设置为点击背景可以移动窗口
[self setMovableByWindowBackground:YES];

1.6 关闭当前window
[self.view.window close];

 

1.7 创建新的window
(1)创建子类WindowController,继承与NSindowController。

(2)创建新的Window

self.windowController = [[WindowController alloc]initWithWindowNibName:@"WindowController"];

[self.windowController showWindow:self];

一定要定义成全局的

 

1.8 NSWindow实现圆角
(1)子类化NSWindow,主要是重载了下面这个函数

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
    
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing: NSBackingStoreBuffered defer:NO];
    
    if (self != nil) {
        
        [self setOpaque:NO];
        
        [self setBackgroundColor:[NSColor clearColor]];
        
    }
    
    return self;
    
}



(2)子类化NSWindow的view,重载drawRect,其中的圆角半径和背景颜色 自己可以调整

- (void)drawRect:(NSRect)dirtyRect {
    
    [NSGraphicsContext saveGraphicsState];
    
    NSRect rect = [self bounds];
    
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:5 yRadius:5];
    
    [path addClip];
    
    [[NSColor controlColor] set];
    
    NSRectFill(dirtyRect);
    
    [NSGraphicsContext restoreGraphicsState];
    
    [super drawRect:dirtyRect];
    
}

 

1.9 监听window窗口大小
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:nil];


- (void)windowDidResize:(NSNotification *)notification {
    
    //NSLog(@"notification %@",notification.object);
    
    NSWindow *window = notification.object;
    
    NSLog(@"window %@",NSStringFromRect(window.frame));
    
}

 

1.10 设置window不可拉伸
xib 中取消选择Resize。

 



 



 

posted @ 2022-06-25 23:47  极度恐慌_JG  阅读(237)  评论(0编辑  收藏  举报