macOS开发暗黑模式适配

522 阅读1分钟

关于macOS的暗黑模式,我从以下两个方面去讲。因为这两个是我们开发中搭建UI界面最常用的控件。

苹果官方文档: developer.apple.com/documentati…

请按照顺序阅读!!!

ColorSet的创建

image.png

命名为MyColor

image.png

设置左边的颜色值(白色主题下显示的) 设置右边边的颜色值(黑色主题下显示的)

image.png

NSView的适配

针对自定义的View,你可以在下面的这些方法中去动态设置你View的颜色

updateLayer

drawRect:

layout

updateConstraints

示例代码如下:

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [NSColor colorNamed:@"MyColor"].CGColor;
}

NSViewController的适配

由于NSViewController的特殊性,目前测试只有在updateViewConstraints方法中修改颜色才生效。

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [NSColor colorNamed:@"MyColor"].CGColor;
}