iOS Question wait until layout is completely loaded

D

Deleted member 103

Guest
Hi,

I load a layout in a root panel (because of iPhone-X), but it always takes different amounts of time.
How can I wait until a layout is completely loaded?
I use this code, but sometimes 100 ms are sufficient and sometimes 500ms are necessary.

B4X:
Private Sub Page1_Resize(Width As Int, Height As Int)
    Log("Page1_Resize")

    If pnlRoot.IsInitialized Then
        Dim r As Rect = Page1.SafeAreaInsets
        pnlRoot.SetLayoutAnimated(0, 1, r.Left, r.Top, Width - r.Right - r.Left, Height - r.Bottom - r.Top)
        Sleep(100)
    End If
...
End Sub
 
D

Deleted member 103

Guest
That's true. You don't need to wait at all.

There are too things that might confuse here:
1. The layout animation. You can set the duration to 0 if you like.
2. The page being resized.
If I load the layout normally with Page1.RootPanel.LoadLayout ("Layout1"), then everything works without problems.

Only when I load the layout with this code does it make problems.
B4X:
Sub Process_Globals
   ....
    Private pnlRoot As Panel
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   pnlRoot.Initialize("")
   Page1.RootPanel.AddView(pnlRoot, 0, 0, 200, 200)

   InitApp
   ...
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    Log("Page1_Resize")

    If pnlRoot.IsInitialized Then
        Dim r As Rect = Page1.SafeAreaInsets
        pnlRoot.SetLayoutAnimated(0, 1, r.Left, r.Top, Width - r.Right - r.Left, Height - r.Bottom - r.Top)
        Sleep(100)
    End If
...
End Sub

Private Sub InitApp
    If pnlRoot.IsInitialized Then
       pnlRoot.RemoveAllViews
       If IsPadDevice Then
           pnlRoot.LoadLayout("frmMainIpad")
           SetOrientation(3)
       else if IsPhonePlus And manager.GetString("rbtOrientationModus") = language.value("strLandscape") Then
            pnlRoot.LoadLayout("frmMainIpad")
           SetOrientation(3)
       Else
           pnlRoot.LoadLayout("frmMainIphone")
           SetOrientation(1)
       End If
    Else
       Page1.RootPanel.RemoveAllViews
       If IsPadDevice Then
           Page1.RootPanel.LoadLayout("frmMainIpad")
           SetOrientation(3)
       else if IsPhonePlus And manager.GetString("rbtOrientationModus") = language.value("strLandscape") Then
           Page1.RootPanel.LoadLayout("frmMainIpad")
           SetOrientation(3)
       Else
           Page1.RootPanel.LoadLayout("frmMainIphone")
           SetOrientation(1)
       End If
   End If
End Sub

Public Sub IsPhonePlus As Boolean
   If GetDeviceLayoutValues.DeviceApproximateScreenSize > 5 Then
       Return True
   Else
       Return False
   End If
End Sub

Public Sub SetOrientation(landscape As Int)
   Dim no As NativeObject
   no.Initialize("UIDevice").RunMethod("currentDevice", Null).SetField("orientation", landscape)
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Hard to say from this code. Try to reproduce it in a small project and upload it.
I have already tried it with a test project, unfortunately I could not reproduce the error.
If you want then I send you my project.
 
Upvote 0
D

Deleted member 103

Guest
Hard to say from this code. Try to reproduce it in a small project and upload it.
Now I have made an example to reproduce the problem.
The layout is very simple in this example, so works from 10ms without problems, with complicated layout is therefore more time necessary.
In my project, it was about 300ms necessary (test with a simulator iPhone-X).

Test with Page1.RootPanel.LoadLayout("frmMainIpad"):
Page1.RootPanel.png


pnlRoot.LoadLayout("frmMainIpad") and sleep(5)
rootpanel_sleep(5).png


pnlRoot.LoadLayout("frmMainIpad") and sleep(10)

rootpanel_sleep(10).png
 

Attachments

  • RootpanelExample.zip
    3.2 KB · Views: 214
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. What do you need this lbl for? It doesn't seem to do anything useful.

2. The problem here happens because you are handling the wrong resize event.

As the layout was added to pnlRoot you should handle pnlRoot resize event:
B4X:
pnlRoot.Initialize("pnlRoot")


Private Sub Page1_Resize(Width As Int, Height As Int)
   Log("Page1_Resize")
   If pnlRoot.IsInitialized Then
       Dim r As Rect = Page1.SafeAreaInsets
       pnlRoot.SetLayoutAnimated(0, 1, r.Left, r.Top, Width - r.Right - r.Left, Height - r.Bottom - r.Top)
   End If

End Sub

Sub pnlRoot_Resize (Width As Float, Height As Float)
   'Alle Labels richtig positionieren
   SetupTextFonts
End Sub
 
Upvote 0
D

Deleted member 103

Guest
1. What do you need this lbl for? It doesn't seem to do anything useful.
I did that only for this example. ;)

2. The problem here happens because you are handling the wrong resize event.
Thank you, it's now clear to me. :)
 
Upvote 0
D

Deleted member 103

Guest
As the layout was added to pnlRoot you should handle pnlRoot resize event:
It can not be, you're right again!
It even works without the sleep(xx) function. ;)
 
Last edited by a moderator:
Upvote 0
Top