Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pookjw/0612cfcfff50c0f5a4d243e632741dea to your computer and use it in GitHub Desktop.
Save pookjw/0612cfcfff50c0f5a4d243e632741dea to your computer and use it in GitHub Desktop.
// UIWindowScene+interfaceOrientationDidChangeNotification.h
#import <UIKit/UIKit.h>
NS_HEADER_AUDIT_BEGIN(nullability, sendability)
static NSNotificationName const UIWindowSceneInterfaceOrientationDidChangeNotification = @"UIWindowSceneInterfaceOrientationDidChangeNotification";
static NSString * const UIWindowSceneInterfaceOrientationValueUserInfoKey = @"UIWindowSceneInterfaceOrientationValueUserInfoKey";
static NSString * const UIWindowSceneInterfaceOrientationAnimationDurationUserInfoKey = @"UIWindowSceneInterfaceOrientationAnimationDurationUserInfoKey";
NS_HEADER_AUDIT_END(nullability, sendability)
// UIWindowScene+interfaceOrientationDidChangeNotification.mm
#import "UIWindowScene+interfaceOrientationDidChangeNotification.h"
#import <objc/runtime.h>
#import <objc/message.h>
namespace UIWindowScene_Booth_Category {
namespace _updateClientSettingsToInterfaceOrientation_withAnimationDuration {
static void (*original)(UIWindowScene *self, SEL _cmd, UIInterfaceOrientation interfaceOrientation, CGFloat animationDuration);
static void custom(UIWindowScene *self, SEL _cmd, UIInterfaceOrientation interfaceOrientation, CGFloat animationDuration) {
original(self, _cmd, interfaceOrientation, animationDuration);
[NSNotificationCenter.defaultCenter postNotificationName:UIWindowSceneInterfaceOrientationDidChangeNotification
object:self
userInfo:@{
UIWindowSceneInterfaceOrientationValueUserInfoKey: @(self.interfaceOrientation),
UIWindowSceneInterfaceOrientationAnimationDurationUserInfoKey: @(animationDuration)
}];
}
static void swizzle() {
Method method = class_getInstanceMethod(UIWindowScene.class, NSSelectorFromString(@"_updateClientSettingsToInterfaceOrientation:withAnimationDuration:"));
original = reinterpret_cast<void (*)(UIWindowScene *, SEL, UIInterfaceOrientation, CGFloat)>(method_getImplementation(method));
method_setImplementation(method, reinterpret_cast<IMP>(&custom));
}
}
}
@implementation UIWindowScene (Booth_Category)
+ (void)load {
UIWindowScene_Booth_Category::_updateClientSettingsToInterfaceOrientation_withAnimationDuration::swizzle();
}
@end
@pookjw
Copy link
Author

pookjw commented Sep 24, 2023

Example

class View: UIView {
    private var interfaceOrientationRegistration: NSObjectProtocol?
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        if let windowScene: UIWindowScene = window?.windowScene {
            interfaceOrientationRegistration = NotificationCenter.default.addObserver(forName: .UIWindowSceneInterfaceOrientationDidChange, object: windowScene, queue: nil) { notification in
                print(notification)
            }
        } else {
            interfaceOrientationRegistration = nil
        }
    }
}

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