Home > Back-end >  Lock orientation with native Objective-C code from Qt
Lock orientation with native Objective-C code from Qt

Time:12-21

I want to be able to change orientation only programmatically. Wrote function, which rotates screen, as I want.

UIInterfaceOrientation toIOSOrientation(ScreenOrientation desiredOrientaion) {
    if (desiredOrientaion == ScreenOrientation::Landscape || desiredOrientaion == ScreenOrientation::LandscapeReversed) {
        return UIInterfaceOrientation::UIInterfaceOrientationLandscapeLeft;
    } else if (desiredOrientaion == ScreenOrientation::Portrait) {
        return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
    } else {
        return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
    }
}

void iOSTools::changeOrientation(ScreenOrientation desiredOrientaion) {
    NSNumber* value = [NSNumber numberWithInt:toIOSOrientation(desiredOrientaion)];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    [UIViewController attemptRotationToDeviceOrientation];
}

But I have to turn on my two orientation in application manifest:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
</array>

If I'm doing so, user is able to use both of this orientations. If I want to forbid this, I have to override this function, but how I should do it in Qt?

CodePudding user response:

    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
            if (shouldRotate == YES){
               return UIInterfaceOrientationMaskAll;
            }else{
              return UIInterfaceOrientationMaskPortrait;
            }
       }

In app delegate, you can try to add this function taking should rotate as global value and whenever you apply the orientation lock, we can make should rotate as false, it will be in portrait mode. If you don't apply orientation lock, we can make rotate as true, then it can rotate in all states.

  • Related