1. 程式人生 > >iOS 導致橫屏失敗的bug 的原因分析之一

iOS 導致橫屏失敗的bug 的原因分析之一

// - 有時我們呼叫了這個方法 並且實現了螢幕旋轉相關的程式碼但是螢幕還是沒有旋轉過來 有可能導致這個問題的原因是我們多次呼叫了移除
[[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; }