iOS 16之前,横竖屏切换的时候会自动调用以下方法:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
iOS 16之后,该方法时而灵时而不灵,就很烦人,那么就需要手动监听屏幕旋转,方法如下:
我暂时将该方法放到viewWillAppear中,
if (@available(iOS 16.0, *)) {
if(IS_Pad){
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
}
}
viewWillDisappear
if (@available(iOS 16.0, *)) {
if(IS_Pad){
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
}
- (void)didRotate:(NSNotification *)noti{
if (@available(iOS 16.0, *)) {
[self setNeedsUpdateOfSupportedInterfaceOrientations];
} else {
// Fallback on earlier versions
}
}
如果有基类的话,把上述方法放到基类里面最好,设置一处全局都可以使用了。 如果有问题,可以后台私信