@@ -74,7 +74,6 @@ class UIViewControllerImpl extends UIViewController {
7474
7575 public isBackstackSkipped : boolean ;
7676 public isBackstackCleared : boolean ;
77- private didFirstLayout : boolean ;
7877 // this is initialized in initWithOwner since the constructor doesn't run on native classes
7978 private _isRunningLayout : number ;
8079 private get isRunningLayout ( ) {
@@ -85,7 +84,7 @@ class UIViewControllerImpl extends UIViewController {
8584 }
8685 private finishRunningLayout ( ) {
8786 this . _isRunningLayout -- ;
88- this . didFirstLayout = true ;
87+ this . clearScheduledLayout ( ) ;
8988 }
9089 private runLayout ( cb : ( ) => void ) {
9190 try {
@@ -96,11 +95,31 @@ class UIViewControllerImpl extends UIViewController {
9695 }
9796 }
9897
98+ layoutTimer : number ;
99+
100+ private clearScheduledLayout ( ) {
101+ if ( this . layoutTimer ) {
102+ clearTimeout ( this . layoutTimer ) ;
103+ this . layoutTimer = null ;
104+ }
105+ }
106+
107+ private scheduleLayout ( ) {
108+ if ( this . layoutTimer ) {
109+ return ;
110+ }
111+ setTimeout ( ( ) => {
112+ this . layoutTimer = null ;
113+ if ( ! this . isRunningLayout ) {
114+ this . runLayout ( ( ) => this . layoutOwner ( ) ) ;
115+ }
116+ } ) ;
117+ }
118+
99119 public static initWithOwner ( owner : WeakRef < Page > ) : UIViewControllerImpl {
100120 const controller = < UIViewControllerImpl > UIViewControllerImpl . new ( ) ;
101121 controller . _owner = owner ;
102122 controller . _isRunningLayout = 0 ;
103- controller . didFirstLayout = false ;
104123
105124 return controller ;
106125 }
@@ -281,19 +300,24 @@ class UIViewControllerImpl extends UIViewController {
281300
282301 public viewSafeAreaInsetsDidChange ( ) : void {
283302 super . viewSafeAreaInsetsDidChange ( ) ;
284- if ( this . isRunningLayout || ! this . didFirstLayout ) {
285- return ;
286- }
287- const owner = this . _owner ?. deref ( ) ;
288- if ( owner ) {
289- this . runLayout ( ( ) => IOSHelper . layoutView ( this , owner ) ) ;
290- }
303+ this . scheduleLayout ( ) ;
291304 }
292305
293306 public viewDidLayoutSubviews ( ) : void {
294307 this . startRunningLayout ( ) ;
295308 super . viewDidLayoutSubviews ( ) ;
309+ this . layoutOwner ( ) ;
310+ this . finishRunningLayout ( ) ;
311+ }
312+
313+ layoutOwner ( force = false ) {
296314 const owner = this . _owner ?. deref ( ) ;
315+ if ( ! force && ! ! owner . nativeViewProtected ?. layer . needsLayout ?.( ) ) {
316+ // we skip layout if the view is not yet laid out yet
317+ // this usually means that viewDidLayoutSubviews will be called again
318+ // so doing a layout pass now will layout with the wrong parameters
319+ return ;
320+ }
297321 if ( owner ) {
298322 // layout(owner.actionBar)
299323 // layout(owner.content)
@@ -345,7 +369,6 @@ class UIViewControllerImpl extends UIViewController {
345369
346370 IOSHelper . layoutView ( this , owner ) ;
347371 }
348- this . finishRunningLayout ( ) ;
349372 }
350373
351374 // Mind implementation for other controllerss
0 commit comments