Nativescript - TabView styling - nativescript

I'm new on NativeScript, and I'm experimenting with differents templates that NS offers. I want to style and set "height", "border" and "radius" properties to each one of TabViewItem of a TabView.
I'm playing around fresh {N} Core JavaScript Tab template, created with:
tns create app-name --template tns-template-tab-navigation
The simplyfied code is:
<TabView selectedIndexChanged="onSelectedIndexChanged">
<TabView.items>
<TabViewItem title="Tab 1" iconSource="res://icon1">
<TabViewItem.view>
<!-- Content firts tab -->
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Tab 2" iconSource="res://icon2">
<TabViewItem.view>
<!-- Content second tab -->
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
I try to set height and borders inline, using style="", and too modifying css, but nothing works.
Properties like height and margin, works fine when applied to TabView, inline way, but don't work with TabViewItem.
Ideally, I would like to get something like this question but as you can see, nobody has responded.

Related

Use Icon Fonts in TabViewItem - Nativescript-Vue

I have been searching for a clean way to use Font Icons (iconmoon in my case) for TabViewItem but have not found an answer. Does anyone know how to do it?
I am using NativeScript-Vue. The ideal way of doing it would be by changing the iconSource with the font icon, but it does not work. Something like :iconSource="String.fromCharCode(0xea0d)"
The code I am using is the following (instead of labels I have frames inside each TabViewItem).
<TabView :selectedIndex="selectedIndex" iosIconRenderingMode="alwaysOriginal">
<TabViewItem title="Tab 1" iconSource="~/images/icon.png">
<Label text="Content for Tab 1" />
</TabViewItem>
<TabViewItem title="Tab 2" iconSource="~/images/icon.png">
<Label text="Content for Tab 2" />
</TabViewItem>
</TabView>
It's not possible to use font icon on iconSource, it would accept only images.
But you could either use font icon on the title attribute but that may prevent you from styling your title with a different font Or you could use the nativescript-vector-icons which can convert the font icon into image on the fly and apply it to iconSource.

Can Nativescript 4.0 new Frame use more elements than Tabview or Sidedrawer?

I see that Frame works now much better.
We can have Tabview,that is a root of current view.
<TabView androidTabsPosition="bottom">
<TabViewItem title="First">
<Frame defaultPage="home/home-page" />
</TabViewItem>
<TabViewItem title="Second">
<Frame defaultPage="second/second-page" />
</TabViewItem>
</TabView>
This looks like home-page or second-page is “included”.
Now, i’m wondering if it’s possible to have app-root.xml that holds common elements, and needed page is included. I’ve tried this, but this is not working (why? This approach is possible only for tabview and sidedrawer ?)
app-root.xml
<Page>
<Frame defaultPage="create/create"></Frame>
</Page>
create/create.xml
<StackLayout class="footer white">
<Label text="test"></Label>
</StackLayout>
Instead of Page use layout like GridLayout Look at this test application as a reference and more specifically this page
However, the above approach would work for Android but for iOS, you should either remove the action bar for each Page (inside each Frame) or create multiple action bars (not recommended!).

Nativescript tab layout approach

I am trying to create an app with NativeScript with the following layout:
On top, action bar
Content Area in the center (about 80% of screen height)
Fixed menu on bottom with 4 buttons
I know that NativeScript provides a TabView, however this view puts the menu on top for the android version of the app,and doesn't allow to use images as "ItemTitle".
So (I think) this leaves me with two options :
Create 4 different pages and load them when a user taps on the menu
buttons
Create one single page and change content according to user
selection
The first approach is great because I get to separate all xml , js and css files. However navigating between pages can take some time and doesn't give ideal user experience.
The second approach probably will have a better user experience but code will be very hard to maintain.
Which option should I use? Have any of you dealt with a similar layout?
Thank you for your time!
In my app I use the TabView with partial-views in each tab-item. So for each tab-view the content is separated in their own xml,js,css files.
So why not just take this approach and combine it with your option 2?
You could create a main-view with:
ActionBar
Centered main-content for partial-views (Stack- og GridLayout)
SegmentedBar at the bottom for navigation buttons
When the user taps a button on the SegmentedBar, you change the visibility of the corresponding partial-view.
You can use any font-icons as "images" for your title in the SegmentedBar also.
UPDATE: Added examples below.
How to create and reference partial-views
In your main-view Page-element add the references to each partial view, like here:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded"
xmlns:t1="partial-views/tab1"
xmlns:t2="partial-views/tab2"
xmlns:t3="partial-views/tab3">
Each partial view consists of a .xml, .js and perhaps a .css file. I like to place each partial view in separate folders: tab1, tab2, tab3 as an example.
The partial-view xml file would contain only the view-modules, no page-modules. So don't add any Page or ActionBar here. Example of a partial view .xml:
<GridLayout loaded="viewLoaded">
<ListView items="{{ someItemList }}">
<ListView.itemTemplate>
...
</ListView.itemTemplate>
</ListView>
</GridLayout>
How to use partial-views
Now, it's up to you how you want to use the partial-views. Here is an example on how to use them together with a TabView component. This is placed in the same page-view where you added the references from the first example.
<TabView>
<TabView.items>
<TabViewItem title="Tab 1" iconSource="res://tab1">
<TabViewItem.view>
<t1:tab1 />
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Tab 2" iconSource="res://tab2" >
<TabViewItem.view>
<t2:tab2 />
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Tab 3" iconSource="res://tab3" >
<TabViewItem.view>
<t3:tab3 />
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
Or, you could do it without the TabView, and create something custom:
<StackLayout>
<t1:tab1 id="tab1" visibility="visible" />
<t2:tab2 id="tab2" visibility="collapsed" />
<t3:tab3 id="tab3" visibility="collapsed" />
</StackLayout>
<SegmentedBar selectedIndex="0" selectedIndexChanged="segBarSelected">
<SegmentedBar.items>
<SegmentedBarItem title="Tab 1" />
<SegmentedBarItem title="Tab 2" />
<SegmentedBarItem title="Tab 3" />
</SegmentedBar.items>
</SegmentedBar>
So here would selectedIndexChangedcontrol the visibility of each partial view.

What is the correct method for accessing the exports object in a TabViewItem?

<Page xmlns:tsk="pages/task" xmlns:trnr="pages/turner" loaded="loaded" navigatingTo="navigatingTo">
<Page.actionBar>
<ActionBar>
<ActionBar.actionItems>
<ActionItem text="Add" ios.position="right" android.position="popup" tap="addTabTapped" />
</ActionBar.actionItems>
</ActionBar>
</Page.actionBar>
<TabView>
<TabView.items>
<TabViewItem title="Turners">
<TabViewItem.view>
<trnr:turner-list-page />
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Task">
<TabViewItem.view>
<tsk:task-list-page />
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
</Page>
I have a TabView xml page that contains custom xml components as TabViewItems. Each of these components has a code behind page with navigatingTo methods as exports. In my TabView loaded method, I have a handler that sets the currently selected TabViewItem "onSelectedIndexChange". When the user is navigated around the application, and back to the TabView page, the navigatingTo that is invoked is in the TabView, not in the TabViewItems. I have tried to place the navigatingTo in various spots in the TabView xml page, but the navigatingTo only seems to be called for the page, not the custom xml components.
I see when debugging and watching the TabViewItem that the exports object is available, and that the exports of each TabViewItem are accessible, but when I try to invoke those methods from within my code, they do not seem to be accessible.
Is there a means of accessing the export object on a TabViewItem so that the exported methods within the various tab view items can be invoked?
So I have successfully managed to answer my own question:
In my loaded function:
export function loaded(args:EventData) {
var page:Page = <Page>args.object;
var tv:tabView.TabView = <tabView.TabView>page.content;
tv.on(tabView.TabView.selectedIndexChangedEvent, handleTabChange);
selectedTabViewItem = tv.items[tv.selectedIndex];
};
I set a reference to the selected tab view item.
Then to reference the exports method, it is as simple as
selectedTabViewItem.view["exports"].addTapped(args);
Anything that is exported can now be accessed from anything referencing that view.

TabView vs SegmentedBar

I am creating SegmentedBar in native script. I am able to create segments but I am not able to add Label to segment view.
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<StackLayout>
<SegmentedBar>
<SegmentedBar.items>
<SegmentedBarItem title="Segment 1">
<SegmentedBarItem.view>
<Label text=" I am in segment bar 1"/>
</SegmentedBarItem.view>
</SegmentedBarItem>
<SegmentedBarItem title="Segment 2">
<SegmentedBarItem.view>
<Label text=" I am in segment bar 2"/>
</SegmentedBarItem.view>
</SegmentedBarItem>
</SegmentedBar.items>
</SegmentedBar>
</StackLayout>
</Page>
What is the difference between SegmentedBar and TabView as both appear same.
The Segmented bar is described in a good way by Apple:
A segmented control is a horizontal control made of multiple segments,
each segment functioning as a discrete button.
So basically: A Segmented Bar is a couple of buttons (visually) connected to each other. Just think of them like buttons with a specific look.
A TabView on the other hand the tabs (the items you click) and a connected view to each tab.
What you're doing in your code is that you're trying to combine mechanics of the TabView with the SegmentedBar.
Take a look at these two code examples.
First, the SegmentedBar. Here is an example. When you click the "First", "Second" or "Third" button nothing will happen. To react on a button press you've to bind the selectedIndex to an Observable object property and do your logic in the on the propertyChange event.
<SegmentedBar selectedIndex="{{ selectedIndex }}">
<SegmentedBar.items>
<SegmentedBarItem title="First" />
<SegmentedBarItem title="Second" />
<SegmentedBarItem title="Third" />
</SegmentedBar.items>
</SegmentedBar>
The TabView, on the other hand, consist of two things, the tabs themselves (the things you press) and a View connected to each tab. So when you click a tab the view gets changed.
<TabView>
<TabView.items>
<TabViewItem title="Tab 1">
<TabViewItem.view>
<Label text="Label in Tab1" />
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Tab 2">
<TabViewItem.view>
<Label text="Label in Tab2" />
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
These two components are used for different things. E.g. for filtering a list (show all mails, show only unread mails...) you usually use the segmented bar as you don't want to change the view - you want to change the content of the view. The TabView is used for when you actually want to display a whole new view.
You may know the <TabView> is created keeping in mind to show different pages/views in single page/view. Thus, TabView is used mainly for navigating to different views.
The SegmentedBar is created for different purpose. This can be used in a view with different functionality for example you can categorize the contents/products as Free, Paid. You would want to show different product features, services on Free view and Paid view. Thus, you can use the SegmentedBar to show different options for the user.
So, now you know the difference between the TabView and the SegmentedBar.
The correct use of SegmentedBar is to use like this:
<SegmentedBar>
<SegmentedBar.items>
<SegmentedBarItem title="Free" />
<SegmentedBarItem title="Paid" />
</SegmentedBar.items>
</SegmentedBar>
To conclude, use TabView for navigating multiple pages in single page and use SegmentedBar for viewing different content in a single view.

Resources