iOS 导致横屏失败的bug 的原因分析之一

本文探讨了iOS应用中常见的屏幕旋转问题,包括移除通知不当导致的问题及未正确调用shouldAutorotate方法的情况。提供了代码示例来解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// - 有时我们调用了这个方法 并且实现了屏幕旋转相关的代码但是屏幕还是没有旋转过来 有可能导致这个问题的原因是我们多次调用了移除
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
这个通知的问题 如果加入一次通知 移除一次通知没有问题,但是如果加入过一次通知但是移除通知两次,就会出现问题

2.有时候我们应该悬转的视图控制没有调用 shouldAutorotate 方法 导致视图控制一直没有旋转 那么可能是 下边的方法中一直返回 UIInterfaceOrientationMaskAllButUpsideDown 导致没有调用系统的 shouldAutorotate 方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    if (self.supportLandscape) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];

#pragma mark -  设备旋转
-(BOOL)shouldAutorotate{
	return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值