Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable 77% scaling for Mac Catalyst apps. (Swift) (Works on macOS 12)
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 }
}
@JunyuKuang
Copy link
Author

For implementation of swizzleInstanceMethod, check Swizzle.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment