Skip to content

Instantly share code, notes, and snippets.

@JunyuKuang
Last active September 29, 2024 00:58
Show Gist options
  • Save JunyuKuang/75fcb81fd6046c0bf31933d85043d04b to your computer and use it in GitHub Desktop.
Save JunyuKuang/75fcb81fd6046c0bf31933d85043d04b to your computer and use it in GitHub Desktop.
Disable 77% scaling for Mac Catalyst apps. (Swift)
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 }
}
@wangwanjie
Copy link

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?

@beeradmoore
Copy link

Is any of this considered using a private API and risk rejectoin from the App Store?

@JunyuKuang
Copy link
Author

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