@@ -2,6 +2,7 @@ import { SplitViewBase, displayModeProperty, splitBehaviorProperty, preferredPri
22import { View } from '../core/view' ;
33import { layout } from '../../utils' ;
44import { SDK_VERSION } from '../../utils/constants' ;
5+ import { FrameBase } from '../frame/frame-common' ;
56import type { SplitRole } from '.' ;
67
78@NativeClass
@@ -93,6 +94,37 @@ export class SplitView extends SplitViewBase {
9394 return this . viewController . view ;
9495 }
9596
97+ onLoaded ( ) : void {
98+ super . onLoaded ( ) ;
99+ // Ensure proper view controller containment
100+ this . _ensureViewControllerContainment ( ) ;
101+ }
102+
103+ private _ensureViewControllerContainment ( ) : void {
104+ if ( ! this . viewController ) {
105+ return ;
106+ }
107+
108+ const window = this . nativeViewProtected ?. window ;
109+
110+ if ( ! window ) {
111+ return ;
112+ }
113+
114+ const currentRootVC = window . rootViewController ;
115+
116+ // If the window's rootViewController is not our SplitViewController,
117+ // we need to set it up properly
118+ if ( currentRootVC !== this . viewController ) {
119+ // Check if we can become the root view controller
120+ // or if we need to be added as a child
121+ if ( currentRootVC ) {
122+ currentRootVC . addChildViewController ( this . viewController ) ;
123+ this . viewController . didMoveToParentViewController ( currentRootVC ) ;
124+ }
125+ }
126+ }
127+
96128 disposeNativeView ( ) : void {
97129 super . disposeNativeView ( ) ;
98130 this . _controllers . clear ( ) ;
@@ -256,6 +288,36 @@ export class SplitView extends SplitViewBase {
256288 return wrapper ;
257289 }
258290
291+ private _attachSecondaryDisplayModeButton ( ) : void {
292+ const secondary = this . _controllers . get ( 'secondary' ) ;
293+ if ( ! ( secondary instanceof UINavigationController ) ) {
294+ return ;
295+ }
296+
297+ const targetVC = secondary . topViewController ;
298+ if ( ! targetVC ) {
299+ // Subscribe to Frame's navigatedTo event to know when the first page is shown
300+ const frameChild = this . _children . get ( 'secondary' ) as any ;
301+ if ( frameChild && frameChild . on && ! frameChild . _splitViewSecondaryNavigatedHandler ) {
302+ frameChild . _splitViewSecondaryNavigatedHandler = ( ) => {
303+ // Use setTimeout to ensure the navigation controller's topViewController is updated
304+ setTimeout ( ( ) => this . _attachSecondaryDisplayModeButton ( ) , 0 ) ;
305+ } ;
306+ frameChild . on ( FrameBase . navigatedToEvent , frameChild . _splitViewSecondaryNavigatedHandler ) ;
307+ }
308+ return ;
309+ }
310+
311+ // Avoid duplicates
312+ if ( targetVC . navigationItem . leftBarButtonItem ) {
313+ return ;
314+ }
315+
316+ // Set the displayModeButtonItem on the page's navigationItem
317+ targetVC . navigationItem . leftBarButtonItem = this . viewController . displayModeButtonItem ;
318+ targetVC . navigationItem . leftItemsSupplementBackButton = true ;
319+ }
320+
259321 private _attachInspectorButton ( ) : void {
260322 const inspector = this . _controllers . get ( 'inspector' ) ;
261323 if ( ! ( inspector instanceof UINavigationController ) ) {
@@ -264,14 +326,14 @@ export class SplitView extends SplitViewBase {
264326
265327 const targetVC = inspector . topViewController ;
266328 if ( ! targetVC ) {
267- // Subscribe to Frame event to know when the top VC is shown, then attach the button.
268- // Can only attach buttons once VC is available
329+ // Subscribe to Frame's navigatedTo event to know when the first page is shown
269330 const frameChild = this . _children . get ( 'inspector' ) as any ;
270- if ( frameChild && frameChild . on && ! frameChild . _inspectorVCShownHandler ) {
271- frameChild . _inspectorVCShownHandler = ( ) => {
272- setTimeout ( ( ) => this . _attachInspectorButton ( ) ) ;
331+ if ( frameChild && frameChild . on && ! frameChild . _splitViewNavigatedHandler ) {
332+ frameChild . _splitViewNavigatedHandler = ( ) => {
333+ // Use setTimeout to ensure the navigation controller's topViewController is updated
334+ setTimeout ( ( ) => this . _attachInspectorButton ( ) , 0 ) ;
273335 } ;
274- frameChild . on ( 'viewControllerShown' , frameChild . _inspectorVCShownHandler . bind ( this ) ) ;
336+ frameChild . on ( FrameBase . navigatedToEvent , frameChild . _splitViewNavigatedHandler ) ;
275337 }
276338 return ;
277339 }
@@ -365,10 +427,10 @@ export class SplitView extends SplitViewBase {
365427 const secondary = this . _controllers . get ( 'secondary' ) ;
366428 const supplementary = this . _controllers . get ( 'supplementary' ) ;
367429 const inspector = this . _controllers . get ( 'inspector' ) ;
368- if ( secondary instanceof UINavigationController && secondary . navigationItem ) {
369- // TODO: can add properties to customize this
370- secondary . navigationItem . leftBarButtonItem = this . viewController . displayModeButtonItem ;
371- secondary . navigationItem . leftItemsSupplementBackButton = true ;
430+
431+ // Attach displayModeButtonItem to the secondary column's first page
432+ if ( secondary instanceof UINavigationController ) {
433+ this . _attachSecondaryDisplayModeButton ( ) ;
372434 }
373435 if ( supplementary ) {
374436 this . showSupplementary ( ) ;
0 commit comments