MAC:NSWindow

目錄
  1.1 隱藏標(biāo)題欄
  1.2 窗口無邊界
  1.3 窗口透明
  1.4 背景紅色
  1.5 點擊背景移動窗口
  1.6 關(guān)閉當(dāng)前window
  1.7 創(chuàng)建新window
  1.8 實現(xiàn)圓角
  1.9 監(jiān)聽窗口大小
  1.10 設(shè)置window不可拉伸
1.1 隱藏NSWindow的標(biāo)題欄

方法一:

(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 設(shè)置窗口無邊界

[self setStyleMask:NSBorderlessWindowMask];
1.3 設(shè)置窗口為透明

[self setOpaque:YES];
1.4 設(shè)置背景紅色

[self setBackgroundColor:[NSColor redColor]];
1.5 設(shè)置為點擊背景可以移動窗口

[self setMovableByWindowBackground:YES];
1.6 關(guān)閉當(dāng)前window

[self.view.window close];
1.7 創(chuàng)建新的window

(1)創(chuàng)建子類WindowController,繼承與NSindowController。

(2)創(chuàng)建新的Window

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

[self.windowController showWindow:self];

一定要定義成全局的

1.8 NSWindow實現(xiàn)圓角

(1)子類化NSWindow,主要是重載了下面這個函數(shù)

- (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,其中的圓角半徑和背景顏色 自己可以調(diào)整

- (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 監(jiān)聽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 設(shè)置window不可拉伸

xib 中取消選擇Resize。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容