您可以通过使用一些低级的 Objective-C 魔法(方法调配)来强制整个应用程序使用覆盖滚动条:
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
static IMP old_preferredScrollerStyle = NULL;
static NSScrollerStyle new_preferredScrollerStyle(id self, SEL _cmd) {
// Always prefer overlay style.
return NSScrollerStyleOverlay;
}
static IMP old_setScrollerStyle = NULL;
static void new_setScrollerStyle(id self, SEL _cmd, NSScrollerStyle style) {
// Call old implementation but always with overlay style.
void(*oldImp)(id self, SEL _cmd, NSScrollerStyle style)
= (void(*)(id, SEL, NSScrollerStyle))old_setScrollerStyle;
oldImp(self, _cmd, NSScrollerStyleOverlay);
}
/// Force the overlay style scrollers for this app.
@interface NSScrollView (ForceOverlay)
@end
@implementation NSScrollView (ForceOverlay)
+ (void)load
{
[super load];
// Replace the preferred style. This sets the style for app startup and new NSScroller
// and NSScrollView instances.
Method originalMethod = class_getClassMethod(
[NSScroller class],
@selector(preferredScrollerStyle)
);
old_preferredScrollerStyle = method_setImplementation(
originalMethod,
(IMP)new_preferredScrollerStyle
);
// Replace the NSScrollView setter. This prevents the change to the legacy style, for example
// when the user switches the system setting.
originalMethod = class_getInstanceMethod(
[NSScrollView class],
@selector(setScrollerStyle:)
);
old_setScrollerStyle = method_setImplementation(
originalMethod,
(IMP)new_setScrollerStyle
);
}
@end