如何解决在macOSObjective-C上的模式更改暗模式,亮模式下以编程方式更新颜色设置
我使用的是macOS,objective-c,而不是iOS。 XCode 12。
在很多视图中,我都设置如下颜色:
self.menuIconBar.wantsLayer = YES;
self.menuIconBar.layer.backgroundColor = [NSColor colorNamed:@"color_gradient_right"].CGColor;
每当用户更改外观时,例如进入深色模式,我希望我的颜色会根据素材资源设置而改变:
很遗憾,没有任何反应。 但:IB中应用的相同颜色会按预期直接更改。仍然我也需要它们以编程方式进行更改。
然后我尝试挂接到通知:
[[NSdistributedNotificationCenter defaultCenter] addobserver:self selector:@selector(appleInterfaceThemeChangednotification:) name:@"AppleInterfaceThemeChangednotification" object:nil];
我收到通知,但是当我再次像上面一样调用相同的代码时,仍然加载了错误的颜色。
self.menuIconBar.layer.backgroundColor = [NSColor colorNamed:@"color_gradient_right"].CGColor;
任何帮助表示赞赏
解决方法
以下示例将根据“系统偏好设置”中的“外观”设置更改自定义视图的背景颜色。可以通过创建objc项目,删除预先存在的App Delegate并用以下代码替换“ main.m”中的代码,在Xcode中运行它:
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
@end
@implementation CustomView
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (void)drawRect:(NSRect)rect {
}
- (void)viewDidChangeEffectiveAppearance {
NSLog (@"appearance did change.");
NSAppearance *changedAppearance = NSApp.effectiveAppearance;
NSAppearanceName newAppearance = [changedAppearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua,NSAppearanceNameDarkAqua]];
NSLog (@"new appearance name = %@",newAppearance);
if([newAppearance isEqualToString:NSAppearanceNameDarkAqua]){
[[self layer] setBackgroundColor:CGColorCreateGenericRGB( 1.0,0.0,1.0 )];
} else {
[[self layer] setBackgroundColor:CGColorCreateGenericRGB( 0.0,1.0,1.0 )];
}
}
// Use this if you want 0,0 (origin) to be top,left
// Otherwise origin will be at bottom,left (Unflipped)
-(BOOL)isFlipped {
return YES;
}
@end
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
- (void) buildMenu;
- (void) buildWindow;
@end
@implementation AppDelegate
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [NSMenu new];
NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"];
[appMenu addItem:quitMenuItem];
[menuBarItem setSubmenu:appMenu];
}
- (void) buildWindow {
#define _wndW 600
#define _wndH 550
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0,_wndW,_wndH )
styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable
backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle: @"Test window"];
[window makeKeyAndOrderFront: nil];
// **** CustomView **** //
CustomView *view = [[CustomView alloc]initWithFrame:NSMakeRect( 20,60,_wndW - 40,_wndH - 80 )];
[view setWantsLayer:YES];
[[view layer] setBackgroundColor:CGColorCreateGenericRGB( 0.0,1.0 )];
[[window contentView] addSubview:view];
// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50,10,40,40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWindow];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification {
}
@end
int main() {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。