Frame
The Frame class represents the logical unit that is responsible for navigation between different pages. An application can have single or multiple Frame instances depending on the business logic and requirements.
Frame Creation
The frame should have the defaultPage property set (mandatory). The page passed as value for defaultPage will load on frame initialization.
The id property can be used to get a reference to the frame instance.
<Frame id="root-frame" defaultPage="main-page"/>
More complex application structure can be created by using multiple frame instances.For example, you can create a TabView while using different frames for each tab item.
<TabView>
<TabViewItem title="First">
<Frame id="firstFrame" defaultPage="home/home-page" />
</TabViewItem>
<TabViewItem title="Second">
<Frame id="secondFrame" defaultPage="second/second-page" />
</TabViewItem>
</TabView>
Frame Reference
The navigation in NativeScript is based on the Frame API and using navigate method of the wanted frame.
To get a reference to the Frame instance you need use the following methods or properties:
- the
topmostmethod from thetns-core-modules/ui/framemodule. The method returns the last navigatedFrameinstance or in case you are in aTabView, the currently selected tab item'sFrameinstance. For more complex cases or more control, you should use methods likegetFrameByIdor theframeproperty ofPageclass.
- the
getFrameByIdmethod from thetns-core-modules/ui/framemodule. This method allows you to get a reference to aFrameby a id that you specified on the element. Note that this searches for already navigated frames and won't find frames that are not yet displayed like in a modal view for example.
- the
frameproperty ofPageinstance. EachPageinstance carries information about the frame object which navigated to it in theframeproperty. This lets you navigate with theframeproperty as well.
Note We can get a reference to a
Frameonly for a frame that has been already loaded in the visual tree. Frames that are not still loaded (for example aFramewithin a modal page that is not yet opened) can not be retrieved.
Basic Navigation
Default page
To load a default (initial) page in your application use the defaultPage property of the Frame element.
With the example below the applicaiton will load a page located in <project-folder>/app/home/first-page.xml
<Frame id="my-frame-id" defaultPage="home/first-page"/>
Navigate by page name
To navigate between pages, you can use the navigate method of the desired Frame instance.
Navigate by NavigationEntry object
The navigate method accepst NavigationEntry object.
Full list of the NavigaitonEntry properties. Note that all of them are optional. Even the moduleName is optional, as alternatively you can pass a dynamically created page via create.
- animated - True to navigate to the new Page using animated transitions, false otherwise.
- backstackVisible - True to record the navigation in the backstack, false otherwise. If the parameter is set to false then the Page will be displayed but once navigated from it will not be able to be navigated back to.
- bindingContext - An object to become the binding context of the page navigating to. Optional.
- clearHistory - True to clear the navigation history, false otherwise. Very useful when navigating away from login pages.
- context - An object passed to the onNavigatedTo callback of the Page. Typically this is used to pass some data among pages. Optional.
- create - A function used to create the View instance. Optional.
- moduleName - The name of the module containing the View instance to load. Optional.
- transition - Specifies an optional navigation transition for all platforms. If not specified, the default platform transition will be used.
- transitionAndroid - Specifies an optional navigation transition for Android. If not specified, the default platform transition will be used.
- transitioniOS - Specifies an optional navigation transition for iOS. If not specified, the default platform transition will be used.
Navigate without history
You can navigate to a page without adding this navigation to the history. Set the backstackVisible property of the NavigationEntry to false.
When the property is set to false, then the Page will be displayed, but once navigated from it will not be able to be navigated back to.
Clear history
You can navigate to a new page and decide to completely clear the entire navigation history. Set the clearHistory property of the NavigationEntry to true.
This will prevent the user from going back to pages previously visited. This is extremely useful if you have a multiple-page authentication process and you want to clear the authentication pages once the user is successfully logged in and redirected to the start page of the application.
Navigation transitions
Transition properties
By default, all navigation will be animated and will use the default transition for the respective platform (UINavigationController transitions for iOS and Fragment transitions for Android). To change the transition type, set the navigationTransition property of the NavigationEntry to an object conforming to the NavigationTransition interface. The NavigationTransition interface has four optional properties:
-
curve - An optional transition animation curve. Possible values are contained in the
AnimationCurveenumeration. Alternatively, you can pass an instance of type UIViewAnimationCurve for iOS or android.animation.TimeInterpolator for Android. - duration - The length of the transition in milliseconds. If you do not specify this, the default platform transition duration will be used.
-
instance - An user-defined instance of the
Transitionclass. -
name - Can be one of the built-in transitions:
- curl (same as curlUp) (iOS only)
- curlUp (iOS only)
- curlDown (iOS only)
- explode (Android Lollipop(21) and up only)
- fade
- flip (same as flipRight)
- flipRight
- flipLeft
- slide (same as slideLeft)
- slideLeft
- slideRight
- slideTop
- slideBottom
Default transition for specific Frame
To specify a default transition for all frame navigations, set the transition property of the frame you are navigating with.
Default transition for the application
To specify a default transition for all navigations across the entire app, set the static defaultTransition property of the Frame class.
Platform-specific transitions
To specify different transitions for the different platforms use the transitioniOS and transitionAndroid properties of the NavigationEntry.
Custom Transitions
Instead of setting the name property to one of the predefined transitions, you can set the instance property of the NavigationTransition to an instance of a class that inherits from Transition.
You can create your custom user-defined transition by writing platform-specific code to animate the transition.
To do that you need to inherit from the Transition class and override one method for each platform.
Since there will be platform-specific code, you need to separate your code into two separate files.
Here is an example of a custom transition that shrinks the disappearing page while expanding the appearing page by using a scale affine transform.
NOTE: The following example uses native APIs. When using TypeScript, you need to add a dev dependency to the
tns-platform-declarationspackage to use these native APIs without compiler errors. For more information, see the Intellisense and access to native APIs via TypeScript section.
custom-transition.android.js/ts
custom-transition.ios.js/ts
Navigate Back
Each frame tracks the pages the user has visited in a navigation stack. To go back to a previous page,
you need to use the goBack method of the current frame instance.
To veify that back navigation is possible, you can use the canGoBack boolean property.
Dynamic Navigation
A more dynamic way of navigating can be done by providing a function that returns the instance of the page to which you want to navigate.
Navigation Context
Passing context during navigation
When you navigate to another page, you can pass context to the page with a NavigationEntry object. The navigaiton entry provides two different (optional) propertes to work with:
- The
contextproperty
- The
bindingContextproperty.
Both properties are used to pass context while navigating, but the bindingContext property will automatically assign the binding context for the landing page.
Retreiving context during navigation
Any context send with bindingContext is automatically assigned as binding context for the navigated (landing) page.
Retreiving a context send through the context property, can be achieved with two different approaches.
- Accessing the
navigationContextproperty of your landingPageinstance. - Using the
navigatedToevent and its arguments of typeNavigatedData.
Action Bar Visibility
Тhе actionBarVisibility is a property that controls the visibility of the Navigation Bar in iOS and the Action Bar in Android for the current Frame. It should be set directly to the Frame and has three option values auto, never, always.
auto- this is the defaultactionBarVisibilityvalue and allows to specify theActionBarvisibility for eachPageindividually.never- this value hides theActionBarfor the currentFrame.always- this one specifies that theActionBarcomponent should always be displayed for the currentFrame. If aPagedoesn't have a declaredActionBar, a default one will be displayed.
Note: If you have set up some of the two properties(
never,always) and you need to hide/show theActionBarimmediately in the currently loaded page, you need to setPage'sactionBarHiddenproperty totrueorfalse.
<Frame id="my-frame-id" actionBarVisibility="never" defaultPage="home/home-page"/>