The most common way to use the navigation bar is as a way of allowing the user to switch between the screens of your app. It emulates the native navigation bars on iOS and Android.
A navigation bar widget can be created by dragging it out from the Tools Palette, where it appears with the following icon:
Alternatively it can be created in script using:
create widget as "com.livecode.widget.navbar"
When used to navigate through an app by switching cards, the navigation bar will typically contain just a few lines of code:
on hiliteChanged
go to card the hilitedItemName of me
end hiliteChanged
Note: it is important to understand the distinction between the name and the label of each navigation item. If you open up the property inspector for the navigation bar, you can change both of them.
The text that is actually displayed for a navigation item, provided the display style is either “text” or “both”, is the label.
The value of the hilitedItemName
property is the name of the currently
highlighted navigation item. This allows multiple items to have the same
label and still be distinguishable from script.
When using the navigation bar in this way, the simplest thing to do is to put the widget into a group and set the group to behave like a background. This will ensure the navigation bar is automatically placed on new cards of the stack. This is how the navigation bar is used (in conjunction with the header bar widget) in the interactive tutorial app.
Individual properties of each navigation item, and the order they are displayed, can be changed using the property inspector.
When one of the icons is clicked, a scrollable icon picker is popped up to allow you to choose a new icon. The current selection is highlighted as shown.
The icon and highlighted icon can be different, to allow further visual feedback about what is currently selected. One way of using this is to turn the navigation bar as a single-selection option control, as might be used in a multiple choice quiz app.
The colors of the navigation bar are controlled by the foreColor
,
backColor
, borderColor
, and hiliteColor
properties as usual for
LiveCode controls.
Aside from responding to a change in the highlight using the
hiliteChanged
message, there is generally not very much that requires
scripting when using the navigation bar. However, each of the various
aspects of the navigation bar items’ display (icon, highlighted icon,
label) can be set individually from script using the itemLabels
,
itemIcons
and hilitedItemIcons
properties. The values of these
properties are just comma-delimited lists. It can be handy to write
utility functions to set these if you are going to do so often, for
example:
command setLabelAtIndex pLabel, pIndex
local tLabels
put the itemLabels of widget "navbar" into tLabels
put pLabel into item pIndex of tLabels
set the itemLabels of widget "navbar" to tLabels
end setLabelAtIndex
Alternatively, it is possible to set all the data of the navbar at once, using the itemArray property.
local tNavItems
put "Contacts" into tNavItems[1]["label"]
put "contacts" into tNavItems[1]["name"]
put "user" into tNavItems[1]["icon_name"]
put "user" into tNavItems[1]["hilited_icon_name"]
...
set the itemArray of widget "navbar" to tNavItems