Saú*_*ril 11 macos appearance objective-c macos-mojave
在macOS 10.14中,用户可以选择采用系统范围的亮或暗外观,我需要根据当前模式手动调整一些颜色.
Dar*_*ust 19
由于您经常通过的实际外观对象effectiveAppearance是复合外观,直接询问其名称可能不是一个可靠的解决方案.
询问currentAppearance通常也不是一个好主意,因为视图可以明确设置为亮模式,或者您想知道视图drawRect:在模式切换后可能获得不正确结果的位置之外是亮还是暗.
我想出的解决方案如下:
BOOL appearanceIsDark(NSAppearance * appearance)
{
if (@available(macOS 10.14, *)) {
NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua,
NSAppearanceNameDarkAqua
]];
return [basicAppearance isEqualToString:NSAppearanceNameDarkAqua];
} else {
return NO;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用它,appearanceIsDark(someView.effectiveAppearance)因为如果您明确设置,特定视图的外观可能与另一个视图的外观不同someView.appearance.
您还可以创建一个类别NSAppearance并添加一个- (BOOL)isDark方法来获取someView.effectiveAppearance.isDark(更好地选择Apple将来不太可能使用的名称,例如通过添加供应商前缀).
Saú*_*ril 16
如果系统是10.14,我已经使用了当前的外观检查
+ (BOOL)isDarkMode {
NSAppearance *appearance = NSAppearance.currentAppearance;
if (@available(*, macOS 10.14)) {
return appearance.name == NSAppearanceNameDarkAqua;
}
return NO;
}
Run Code Online (Sandbox Code Playgroud)
并且在视图中检测模式的变化方法是:
- (void)updateLayer;
- (void)drawRect:(NSRect)dirtyRect;
- (void)layout;
- (void)updateConstraints;
Run Code Online (Sandbox Code Playgroud)
并且为了在视图控制器中检测模式的改变,方法是:
- (void)updateViewConstraints;
- (void)viewWillLayout;
- (void)viewDidLayout;
Run Code Online (Sandbox Code Playgroud)
使用通知:
// Monitor menu/dock theme changes...
[NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
-(void)themeChanged:(NSNotification *) notification {
NSLog (@"%@", notification);
}
Run Code Online (Sandbox Code Playgroud)
有关暗模式文档的更多信息
斯威夫特4
func isDarkMode(view: NSView?) -> Bool {
if #available(OSX 10.14, *) {
if let appearance = view?.effectiveAppearance ?? NSAppearance.current {
return (appearance.name == .darkAqua)
}
}
return false
}
Run Code Online (Sandbox Code Playgroud)
更新:
func isDarkMode(view: NSView) -> Bool {
if #available(OSX 10.14, *) {
return view.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
}
return false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5837 次 |
| 最近记录: |