[iOS] Wrong viewport after calling ofSetOrientation() while in Landscape device orientation

When I try to change orientation by using ofSetOrientation(), it works well if I do it while the device is in a Portrait orientation(OF_ORIENTATION_DEFAULT or OF_ORIENTATION_180).

But if I call ofSetOrientation() while the device is in a Landscape orientation(OF_ORIENTATION_90_LEFT or OF_ORIENTATION_90_RIGHT), the viewport gets in a wrong position.

Here’s a simple example code you can test.
If you touch the screen, the orientation changes in a clockwise order.

in main.mm

make sure to enable hardware orientation as below.

settings.enableHardwareOrientation = true; // enables native view orientation.
settings.enableHardwareOrientationAnimation = false; // enables native orientation changes to be 

in ofApp.mm, copy the following implementations respectively.

//--------------------------------------------------------------
void ofApp::draw(){
	
    ofSetBackgroundColor(0);
    ofSetColor(255, 255, 255);
    ofNoFill();
    const int offset = 100;
    ofDrawRectangle(offset, offset, 0, ofGetWidth()-offset*2, ofGetHeight()-offset*2);
    ofDrawBitmapString("TOP LEFT CORNER", offset, offset);
}

//--------------------------------------------------------------
void ofApp::touchDown(ofTouchEventArgs & touch){

    switch (ofGetOrientation()) { //change orientation in a clockwise order
            
        case OF_ORIENTATION_DEFAULT:
            cout << "OF_ORIENTATION_90_RIGHT" << endl;
            ofSetOrientation(OF_ORIENTATION_90_RIGHT);
            break;
        case OF_ORIENTATION_180:
            cout << "OF_ORIENTATION_90_LEFT" << endl;
            ofSetOrientation(OF_ORIENTATION_90_LEFT);
            break;
        case OF_ORIENTATION_90_LEFT:
            cout << "OF_ORIENTATION_DEFAULT" << endl;
            ofSetOrientation(OF_ORIENTATION_DEFAULT);
            break;
        case OF_ORIENTATION_90_RIGHT:
            cout << "OF_ORIENTATION_180" << endl;
            ofSetOrientation(OF_ORIENTATION_180);
            break;
        default:
            break;
    }
}

//--------------------------------------------------------------
void ofApp::deviceOrientationChanged(int newOrientation){

    switch (newOrientation) {
            
        case OF_ORIENTATION_DEFAULT:
            cout << "OF_ORIENTATION_DEFAULT" << endl;
            ofSetOrientation(OF_ORIENTATION_DEFAULT);
            break;
        case OF_ORIENTATION_180:
            cout << "OF_ORIENTATION_180" << endl;
            ofSetOrientation(OF_ORIENTATION_180);
            break;
        case OF_ORIENTATION_90_LEFT:
            cout << "OF_ORIENTATION_90_LEFT" << endl;
            ofSetOrientation(OF_ORIENTATION_90_LEFT);
            break;
        case OF_ORIENTATION_90_RIGHT:
            cout << "OF_ORIENTATION_90_RIGHT" << endl;
            ofSetOrientation(OF_ORIENTATION_90_RIGHT);
            break;
        default:
            break;
    }
}

And here are screenshots.
If you run the app, it looks like this. (orientation : OF_ORIENTATION_DEFAULT)

When you touch the screen, if will be changed to OF_ORIENTATION_90_RIGHT.

Now, when you actually rotate your device, it will be still OF_ORIENTATION_90_RIGHT.

And then when you touch the device, it changes to OF_ORIENTATION_180 as it should be.
But as you can see, the viewport suddenly gets in a wrong position.

I think this is clearly a bug that should be fixed.
I will try to fix this but in case someone has an idea how to fix this, please let me know. I would really appreciate it.

I figured out how to fix this bug.
I would appreciate if someone can confirm this fix so it can be updated in OF.

https://github.com/openframeworks/openFrameworks/issues/5627