Skip to content

Commit be7f93d

Browse files
committed
feat(ios): SplitView lifecycle improvements
1 parent a7d4799 commit be7f93d

File tree

6 files changed

+81
-23
lines changed

6 files changed

+81
-23
lines changed

apps/automated/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"nativescript-theme-core": "file:../../node_modules/nativescript-theme-core"
1212
},
1313
"devDependencies": {
14-
"@nativescript/android": "alpha",
15-
"@nativescript/ios": "alpha",
16-
"@nativescript/visionos": "~8.9.0",
14+
"@nativescript/android": "~9.0.0",
15+
"@nativescript/ios": "~9.0.0",
16+
"@nativescript/visionos": "~9.0.0",
1717
"@nativescript/vite": "file:../../dist/packages/vite",
1818
"@nativescript/webpack": "file:../../dist/packages/webpack5",
1919
"circular-dependency-plugin": "^5.2.2",

apps/toolbox/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"nativescript-theme-core": "file:../../node_modules/nativescript-theme-core"
1313
},
1414
"devDependencies": {
15-
"@nativescript/android": "alpha",
16-
"@nativescript/ios": "alpha",
17-
"@nativescript/visionos": "~8.9.0",
15+
"@nativescript/android": "~9.0.0",
16+
"@nativescript/ios": "~9.0.0",
17+
"@nativescript/visionos": "~9.0.0",
1818
"@nativescript/vite": "file:../../dist/packages/vite",
1919
"@nativescript/webpack": "file:../../dist/packages/webpack5",
2020
"typescript": "~5.8.0"

apps/toolbox/src/split-view/split-view-secondary.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
2-
<ActionBar title="Secondary View" class="action-bar">
3-
</ActionBar>
42
<!-- Secondary column (detail) -->
53
<StackLayout class="p-16">
64
<Label text="Secondary" marginBottom="12" fontSize="22" fontWeight="bold" />

apps/toolbox/src/split-view/split-view-supplement.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
2-
<ActionBar title="Supplementary View" class="action-bar">
3-
</ActionBar>
42
<!-- Supplementary column (detail) -->
53
<StackLayout class="p-16">
64
<Label text="Supplementary" marginBottom="12" fontSize="22" fontWeight="bold" />

apps/ui/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"nativescript-theme-core": "file:../../node_modules/nativescript-theme-core"
1212
},
1313
"devDependencies": {
14-
"@nativescript/android": "alpha",
15-
"@nativescript/ios": "alpha",
16-
"@nativescript/visionos": "~8.9.0",
14+
"@nativescript/android": "~9.0.0",
15+
"@nativescript/ios": "~9.0.0",
16+
"@nativescript/visionos": "~9.0.0",
1717
"@nativescript/webpack": "file:../../dist/packages/webpack5",
1818
"typescript": "~5.8.0"
1919
},

packages/core/ui/split-view/index.ios.ts

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { SplitViewBase, displayModeProperty, splitBehaviorProperty, preferredPri
22
import { View } from '../core/view';
33
import { layout } from '../../utils';
44
import { SDK_VERSION } from '../../utils/constants';
5+
import { FrameBase } from '../frame/frame-common';
56
import 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

Comments
 (0)