iOS Code Snippet A bit dirty way to give orientation rotation to certain pages in an essentially portrait-only app

Discussion in 'iOS Code Snippets' started by JackKirk, Mar 21, 2019.

  1. JackKirk

    JackKirk Active Member Licensed User

    • You have an essentially portrait-only app.
    • You want to give the user the ability (i.e. option), on certain pages, to change from portrait to landscape or vice versa by rotating the phone - typically for displaying videos or webpages.
    • When the user exits the "rotatable" page you want to "snap back" to portrait.
    There are any number of posts asking for/bemoaning the lack of a means to do this - the closest Erel has come to fulfilling this need is:

    But this doesn't give the "snap back" capability - if you are in landscape when you exit the "rotatable" page that is the orientation you will stay in.

    The following is a variation on the above post which gives the "snap back" capability - with a bit of assistance from the user - the dirty bit.

    In your Main module:
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    Private Sub Process_Globals
    Public Gen_autorotate As Boolean
    End Sub
    Public Sub Main_ShouldAutoRotate As Boolean

    'If auto rotation allowed...
        If Gen_autorotate Then
    'Return True to OBJC code below, allowing auto rotation
            Return True
    'Otherwise, auto rotation to stop...
    'If still in landscape orientation...
            If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
    'Return True to OBJC code below, to continue allowing auto rotation
                'and wait for user to reorient to portrait
                Return True
    End If
    'If got to here must be in portrait orientation and auto rotation not
    'Return False to OBJC code below, disallowing auto rotation
            Return False
    End If

    End Sub

    #if OBJC
    @interface UINavigationController (B4IResize)

    @implementation UINavigationController (B4IResize)
    - (BOOL)shouldAutorotate
      return [(NSNumber*)[B4IObjectWrapper raiseEvent:self :@"_shouldautorotate" :nil] boolValue];

    #End If
    In your "rotatable" page (this is an excerpt from my Webview code):
    Private Sub Event_Webview_page_Resize(Width As Int, Height As Int)

    'Clean up

    'Set up app background
        Webview_page.RootPanel.Color = Colors.White

    'UI_webview is defined and initialized in webviewlayout
    'Set up UI_webview custom view
        UI_webview.BaseView.Top = 0
        UI_webview.BaseView.Left = 
        UI_webview.BaseView.Width = Webview_page.RootPanel.Width
        UI_webview.BaseView.Height = Webview_page.RootPanel.Height

    'Flag auto rotation allowed
        Main.Gen_autorotate = True
    'Load web page
    'Set up navigation back
    00, Webview_page.RootPanel.Width, Webview_page.RootPanel.Height)
        UI_navigation_back.Text = 
    "< Back "
        UI_navigation_back.TextAlignment = UI_navigation_back.ALIGNMENT_LEFT
        UI_navigation_back.Font = 
    Font.CreateNew2(Main.App_preference_general_typeface_normal, .03 * Max(Webview_page.RootPanel.Height, Webview_page.RootPanel.Width))
        UI_navigation_back.Color = 
        UI_navigation_back.TextColor = 
        UI_navigation_back.Visible = 

    End Sub

    Private Sub Event_back_Click

    'Flag auto rotate to stop
        Main.Gen_autorotate = False

    'Clean up
    'Show status bar, see:
        Private wrk_no As NativeObject = Main.App

    'Return to previous page
    End Sub
    Everything hangs on the global boolean Gen_autorotate:
    • When the Webview code is entered it is set to True, which allows the subsequent rotation of the phone by the user to rotate the orientation at will.
    • When the Webview code is exited it is set to False, which allows subsequent rotation of the phone by the user to rotate the orientation - UNTIL the user rotates to portrait mode, then autorotation is blocked - and portrait mode has been "snapped" back to - sort of.
    This all sounds a bit stupid but in practice it actually works quite well - and is superior to the alternatives:
    • Make everything autorotatable.
    • Make nothing autorotatable.
    I hope this is of value to someone...
    Last edited: Mar 22, 2019
  2. JackKirk

    JackKirk Active Member Licensed User

    Last edited: Mar 22, 2019
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice