macOS开发之NSWindow

前言

窗口对象包括titleBar,contentView内容视图,contentBoder底部边框区。
titleBar上面包括控制按钮,标题。


一、contentView 相关

1、设置窗口背景颜色

方法1:window.backgroundColor
- (void)setWindowBKColor {
    [self.window setOpaque:NO];
    [self.window setBackgroundColor:[NSColor cyanColor]];
}
方法2:window.contentView.layer.backgroundColor
    self.window.contentView.layer.backgroundColor = [NSColor cyanColor].CGColor;
    self.window.contentView.wantsLayer = YES;

如果 同时设置上述两种方法,窗口显示为红色,不论设置顺序

self.window.contentView.layer.backgroundColor = [NSColor redColor].CGColor;
self.window.contentView.wantsLayer = YES;

2、设置 window frame/尺寸

[self.window setContentSize:NSMakeSize(infoWindowW, windowHeight)];
NSRect wndFrame = self.window.frame;
[self.window setFrame:NSMakeRect(wndFrame.origin.x, wndFrame.origin.y, infoWindowW, windowHeight) display:YES animate:YES];
self.window.maxSize = NSMakeSize(infoWindowW, windowHeight);
self.window.minSize = NSMakeSize(infoWindowW, windowHeight);

3、设置为点击背景可以移动窗口

  • 如果隐藏了标题栏,点击标题栏位置,仍然可以拖动窗口。但是用户不知道标题栏的位置,所以需要设置点击背景也可以移动。
    [self.window setMovableByWindowBackground:YES];

二、Content Border

默认是none 不显示出来.
需要显示的话可以选择 Small/Large Bottom Border 其他选项
这里我还不知道怎么用代码控制,会的可以告诉我。


三、titleBar

1、设置窗口标题

[self.window setTitle:@"我的 App 标题"];

2、设置标题栏图标

先将 png 格式图片拖到Assets 中,我的图片名为’swift.png’
配置代码如下:

self.window.representedURL = [NSURL URLWithString:@"https://www.baidu.com"];  //
    NSImage *image = [NSImage imageNamed:@"swift"];
    [[self.window standardWindowButton:NSWindowDocumentIconButton] setImage:image];

3、隐藏titlebar

方法1:xib上设置

选中window,取消勾选titleBar。
代码中可以通过 window.hasTitleBar 来了解titleBar是否存在,但是 hasTitleBar 属性为只读,所以不能通过代码设置 hasTitleBar。



添加颜色看看效果
连左上角 关闭、放大等选项也消失


方法2:代码设置
self.window.titlebarAppearsTransparent=YES;
self.window.titleVisibility = NSWindowTitleHidden;


设置window.contentView 的颜色后,显示效果如下:

  • 可以看到关闭、放大选项,titleBar 只是被隐藏,不代表不存在。



4、titleBar和 contentView 融合到一起

xib:在Main.storyboard选中Window,勾选属性Full Size Content View

代码:
self.window.styleMask = self.window.styleMask | NSWindowStyleMaskFullSizeContentView;

  • 不隐藏titleBar


  • 隐藏titleBar



    今天就整理到这里吧

你可能感兴趣的:(macOS开发之NSWindow)