iOS Question Lock current orientation

jai

Active Member
Licensed User
Longtime User
Is there any way to lock current device orientation? I have not been able to find a solution in the forums thus far.
 

b4x-de

Active Member
Licensed User
Longtime User
As far as I can tell this code works on iOS 26:

B4X:
        Dim noPage As NativeObject = pPage
        Dim window As NativeObject
        window = noPage.GetField("window")

        If window.IsInitialized = False Then
            Return
        End If

        Dim windowScene As NativeObject
        windowScene = window.GetField("windowScene")

        If windowScene.IsInitialized = False Then
            Return
        End If

        If pbolLocked = False Then
            ' Unlock orientation
            Dim prefUnlock As NativeObject
            prefUnlock.Initialize("UIWindowSceneGeometryPreferencesIOS")
            prefUnlock = prefUnlock.RunMethod("alloc", Null)
            windowScene.RunMethod("requestGeometryUpdateWithPreferences:errorHandler:", Array(prefUnlock, Null))
            Return
        End If

        ' Lock orientation
        Dim mask As Int

        Select getOrientation
            Case ORIENTATION_PORTRAIT
                mask = 1
            Case ORIENTATION_LANDSCAPE
                mask = 3
            Case ORIENTATION_LANDSCAPE_RIGHT
                mask = 4
            Case Else
                mask = 0
        End Select

        Dim prefLock As NativeObject
        prefLock.Initialize("UIWindowSceneGeometryPreferencesIOS")
        prefLock = prefLock.RunMethod("alloc", Null)
        prefLock = prefLock.RunMethod("initWithInterfaceOrientations:", Array(mask))

        windowScene.RunMethod("requestGeometryUpdateWithPreferences:errorHandler:", Array(prefLock, Null))
 
Upvote 0
Top