Last active
September 29, 2024 00:58
-
-
Save JunyuKuang/75fcb81fd6046c0bf31933d85043d04b to your computer and use it in GitHub Desktop.
Disable 77% scaling for Mac Catalyst apps. (Swift)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let overrideCatalystScaleFactor: Void = { | |
guard let sceneViewClass = NSClassFromString("UINSSceneView") as? NSObject.Type else { | |
return | |
} | |
if sceneViewClass.instancesRespond(to: NSSelectorFromString("scaleFactor")) { | |
// old | |
swizzleInstanceMethod( | |
class: sceneViewClass, | |
originalSelector: NSSelectorFromString("scaleFactor"), | |
swizzledSelector: #selector(swizzle_scaleFactor) | |
) | |
} else { | |
// macOS 11.3 Beta 3+ | |
swizzleInstanceMethod( | |
class: sceneViewClass, | |
originalSelector: NSSelectorFromString("sceneToSceneViewScaleFactor"), | |
swizzledSelector: #selector(swizzle_scaleFactor) | |
) | |
swizzleInstanceMethod( | |
class: sceneViewClass, | |
originalSelector: NSSelectorFromString("fixedSceneToSceneViewScaleFactor"), | |
swizzledSelector: #selector(swizzle_scaleFactor2) | |
) | |
swizzleInstanceMethod( | |
class: NSClassFromString("UINSSceneContainerView"), | |
originalSelector: NSSelectorFromString("sceneToSceneViewScaleForLayout"), | |
swizzledSelector: #selector(swizzle_scaleFactor3) | |
) | |
} | |
}() | |
@objc private extension NSObject { | |
func swizzle_scaleFactor() -> CGFloat { 1 } | |
func swizzle_scaleFactor2() -> CGFloat { 1 } | |
func swizzle_scaleFactor3() -> CGFloat { 1 } | |
} |
I run the code in my AppDelegate.init()
I run the code in my AppDelegate.init()
Thanks, I used your code instead of the OC code I wrote according to your logic, and it works now. I have another question, how did you find these methods to hook? I am considering whether to use this solution, because the API may change when the system is updated, and I don't know if there is any risk of audit for hooking this?
Is any of this considered using a private API and risk rejectoin from the App Store?
Is any of this considered using a private API and risk rejection from the App Store?
Running fine for years. Apple don't cares.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tks, Can you please tell me where to place these code? I have tried in MacOS 14.5, but is doesn't work
`
#import "KGMacCatalystScaleDisabler.h"
#import <KGSafeKit/NSObject+KGSwizzle.h>
@implementation KGMacCatalystScaleDisabler
#if TARGET_OS_MACCATALYST
(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class sceneViewClass = NSClassFromString(@"UINSSceneView");
if (sceneViewClass) {
SEL originalSelector = NSSelectorFromString(@"scaleFactor");
SEL swizzledSelector = @selector(swizzle_scaleFactor);
});
}
(CGFloat)swizzle_scaleFactor {
return 1.0;
}
(CGFloat)swizzle_scaleFactor2 {
return 1.0;
}
(CGFloat)swizzle_scaleFactor3 {
return 1.0;
}
#endif
@EnD
`