Orientation issue with cocos2dx 3.17.2 iOS

I have a game which supports portrait and landscape. I can switch from one and the other without issue when the game is running.
However, on iOS, when the orientation changes while the game is not in the foreground, the orientation is wrong when coming back to the game. The reproduction is as follow:

  • Play the game in landscape
  • Tap home and run the Messages application.
  • While in the Mesages applications, go to portrait mode
  • Go back in the game.
  • Observer the problem.

I debugged a little, and discovered that the difference is that resizeFromLayer is not called (from layoutSubviews) when the issue happens.

Any one got an idea to fix it ?

Can you catch it on application entering foreground? i.e store the apps orientation and when you come back set it to the state it was when it went to the background.

This could very well be a bit that needs to be fixed, but perhaps you can implement a quick fix until it gets officially, assuming that it is a bug, I can’t say.

Hi Frozax.

I faced the same exact issue in the past and i am using cocos2d-x 3.15.1. I could reproduce only on ios 11 and above. You can try the following fix in applicationDidBecomeActive:

 if (@available(iOS 11.0, *)){
    
    const auto visibleSize = cocos2d::Director::getInstance()->getVisibleSize();
    bool isCocosLandscape = visibleSize.width > visibleSize.height;
    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    bool isIOSLandscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
    
    if ( isCocosLandscape != isIOSLandscape ){
        cocos2d::Director* director = cocos2d::Director::getInstance();
        if (cocos2d::Director::getInstance()->isValid()){
            [((CCEAGLView*)director->getOpenGLView()->getEAGLView()) layoutSubviews];
        }
    }
}

Thanks @kerryk for your reply. I was wiling to do something like that, but the documentation (https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews) says we should never call layoutSubviews() directly, and go through layoutIfNeeded instead.

layoutIfNeeded did not work for me. Check your case and if it does not, go with layoutSubviews ) I have been using this solution for a long time now since ios 11 in production with no issues.

@slackmoehrle yes i agree, whatever fix needs to go to engine )

@frozak, please file a bug for this and tag me and include the issue and @kerryk potential work-around so the engineers have as much information for a jumping off point. Thank you. I’ll ask that this gets fixed once you create the issue.