gpt4 book ai didi

macos - NSColor systemColor 在暗/亮模式切换时不改变

转载 作者:行者123 更新时间:2023-12-04 01:05:38 25 4
gpt4 key购买 nike

我正在尝试通过在 NSViewController 中切换暗/亮模式来更改图像的颜色。
我正在使用此代码更改图像的颜色:

- (NSImage *)image:(NSImage *)image withColour:(NSColor *)colour
{
NSImage *img = image.copy;
[img lockFocus];
[colour set];
NSRect imageRect = NSMakeRect(0, 0, img.size.width, img.size.height);
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[img unlockFocus];
return img;
}

我试过从 viewWillLayout 调用这个方法
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];

但似乎系统颜色总是返回相同的 RGB 值。

我也试过监听通知 AppleInterfaceThemeChangedNotification 但即使在这里,RGB 值似乎保持相同 1.000000 0.231373 0.188235
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"AppleInterfaceThemeChangedNotification"
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {

NSLog(@"AppleInterfaceThemeChangedNotification");
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];

NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
NSColor *testColor = [[NSColor systemBlueColor] colorUsingColorSpace:colorSpace];
CGFloat red = [testColor redComponent];
CGFloat green = [testColor greenComponent];
CGFloat blue = [testColor blueComponent];
NSLog(@"%f %f %f", red, green, blue);
}];

我在 NSButtonCell sublass 和覆盖 layout 中工作正常,但无法在 NSViewController 中工作

最佳答案

首先,检查文档部分“使用特定方法更新自定义 View ” here 。它说:

When the user changes the system appearance, the system automatically asks each window and view to redraw itself. During this process, the system calls several well-known methods for both macOS and iOS, listed in the following table, to update your content. The system updates the trait environment before calling these methods, so if you make all of your appearance-sensitive changes in them, your app updates itself correctly.



但是,该表中没有列出 NSViewController 方法。

由于 View 的外观可以独立于当前或“系统”外观,因此对 View Controller 中的外观更改使用react的最佳方法是 KVO View 的 effectiveAppearance 属性,或者在 [NSView viewDidChangeEffectiveAppearance] 中执行某些操作。
- (void)viewDidLoad 
{
[self addObserver:self forKeyPath:@"view.effectiveAppearance" options:0 context:nil];
}

// ...

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if ([keyPath isEqualToString:@"view.effectiveAppearance"])
{
// ...

NSAppearance 有一个 currentAppearance 属性,它独立于系统外观,并由 Cocoa 在上面列出的方法中更新。在其他任何地方,您都需要自己检查是否正确。惯用的方式再次是通过 View 的 effectiveAppearance :
[NSAppearance setCurrentAppearance:someView.effectiveAppearance];

所以,在你的情况下,以下对我来说很有效:
- (void)viewDidLoad 
{
[super viewDidLoad];

[self addObserver:self forKeyPath:@"view.effectiveAppearance" options:0 context:nil];
}

-(void)viewDidLayout
{
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"view.effectiveAppearance"])
{
[NSAppearance setCurrentAppearance:self.view.effectiveAppearance];

self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
}
}

关于macos - NSColor systemColor 在暗/亮模式切换时不改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56968587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com