NOTE! You are browsing legacy documentation. For latest visit docs.nativescript.org.

NativeScript Core

In this article
API Reference
Not finding the help you need?

Modal View

Use the showModal method of the View class to show another view as a modal dialog. You must specify the location of the modal page module. You can provide a context and a callback function that will be called when the modal view is closed. You can also optionally specify whether to show the modal view in fullscreen or not. To close the modal view, you need to subscribe to its shownModally event and store a reference to a close callback function provided by the event arguments. Call this function when you are ready to close the modal view, optionally passing some results to the master page. Here is an example with two views — the main view and a login view. The main one shows the login view modally; the user enters their username and password and when ready clicks the Login button. This closes the modal login view and returns the username/password to the main page which can then log the user in.

TIP: By design on iPhone, a modal view appears only in fullscreen.

Basics

Main view

<Page xmlns="http://www.nativescript.org/tns.xsd">
    <Page.actionBar>
        <ActionBar title="Getting Started"/>
    </Page.actionBar>

    <GridLayout rows="auto, *">

        <Button text="Open modal" tap="openModal" textWrap="true" />

    </GridLayout>
</Page>

Modal view

<Page xmlns="http://www.nativescript.org/tns.xsd" shownModally="onShownModally">
    <StackLayout>
        <GridLayout rows="100 100" columns="* 2*">
            <Label row="0" col="0" text="Username:" textWrap="true" />
            <TextField row="0" col="1" hint="username" text="{{ username }}" />
            <Label row="1" col="0" text="Password:" textWrap="true" />
            <TextField row="1" col="1" hint="password" secure="true" text="{{ password }}" />
        </GridLayout>
        <Button text="SingIn" tap="onLoginButtonTap" />
    </StackLayout>
</Page>

Note: With version 4.0.0 of NativeScript, opening a Modal view from another Modal view is officially supported. The previous versions of NativeScript supported only a single Modal view.


Custom Actionbar

With NativeScript version 4.0.0, we can also show custom ActionBar in the modal view. To do that, we should declare a root frame and to set up our default modal view.

Main page

<Page xmlns="http://www.nativescript.org/tns.xsd">
    <Page.actionBar>
        <ActionBar title="Modal view Navigation"/>
    </Page.actionBar>

    <GridLayout rows="auto, *">

        <Button text="Open modal" tap="openModal" textWrap="true" />

    </GridLayout>
</Page>

Modal root

<Frame defaultPage="ns-ui-category/modal-view/custom-actionbar/modal-view-page"/>

Modal view

<Page backgroundColor="green" showingModally="onShowingModally">
    <Page.actionBar>
        <ActionBar backgroundColor="red" title="Modal view" icon="">
        </ActionBar>
    </Page.actionBar>
    <StackLayout backgroundColor="lightGreen">
        <Label text="Modal view with ActionBar" style="text-align:center;" textWrap="true" />
        <Button text="Close Modal" tap="onCloseModal"/>
    </StackLayout>
</Page>

With NativeScript version 4.0.0 and above, we can navigate within a modal view. We need a root frame defaulting to our first modal view. With the Frame instance, we can navigate within the modal and with the help of closeModal method, we can close the modal from any View instance.

Main page

<Page xmlns="http://www.nativescript.org/tns.xsd">
    <Page.actionBar>
        <ActionBar title="Modal view Navigation"/>
    </Page.actionBar>

    <GridLayout rows="auto, *">

        <Button text="Open modal" tap="openModal" textWrap="true" />

    </GridLayout>
</Page>

Modal root

<Frame defaultPage="ns-ui-category/modal-view/modal-navigation/first-modal-view-page"
       shownModally="onShownModally" 
       showingModally="onShowingModally"/>

First modal view

<Page backgroundColor="green" loaded="onPageLoaded">
    <StackLayout backgroundColor="lightGreen">
        <Button text="Navigate" tap="onNavigate"/>
        <Button text="Close Modal" tap="onCloseModal"/>
    </StackLayout>
</Page>

Second modal view

<Page class="page">
    <StackLayout>
        <Label style="text-align:center;"  text="Second view"/>
        <Button text="Navigate Back" tap="onGoBack"/>
        <Button text="Close Modal" tap="onCloseModal"/>
    </StackLayout>
</Page>