
iPhone TabBar positioned at the bottom of the view
The TabBar, as you can see, is the black bar positioned at the bottom of the screen. It is useful because it remains the same through different views, making users able to easily switch between them.
To create it, just write:
// init the TabBar
window.uicontrols.createTabBar();
// than we create some items, we can add up to five elements
window.uicontrols.createTabBarItem(
"id", // string identifier
"My button", // title displayed
"mappa.png", // icon file
{
onSelect: callback // callback function
}
);
// than we can show the empty TabBar
window.uicontrols.showTabBar();
// after make it visible we show the buttons by specifying the id
window.uicontrols.showTabBarItems("id");
With this methods, we can create how many buttons we would like to have in our app and easily hide/show them.
For example, to add the two buttons displayed above, write on index.html into your project/www folder:
// this is the default function lauched when the device is ready
function onDeviceReady() {
window.uicontrols.createTabBar();
window.uicontrols.createTabBarItem(
"btn1",
"My button",
"icon.png",
{ onSelect: function() { callback('my') } })
window.uicontrols.createTabBarItem(
"btn2",
"My Favourite",
"tabButton:Favorites"
);
window.uicontrols.showTabBar();
window.uicontrols.showTabBarItems("btn1", "btn2");
}
As you can see in the picture above, the second button, even if I called it “My Favorite”, is showed as “Favorites”. This happens because it’s image is not a png, but it is “tabButton:Favorites”. This is a special type of button, which creates a standard system button (so your title will be overwritten). In PhoneGap you can choose between:
tabButton:More
tabButton:Favorites
tabButton:Featured
tabButton:TopRated
tabButton:Recents
tabButton:Contacts
tabButton:History
tabButton:Bookmarks
tabButton:Search
tabButton:Downloads
tabButton:MostRecent
tabButton:MostViewed
But PhoneGap has some other useful methods to manage TabBar buttons.
For example, to make a button selected (really useful!):
window.uicontrols.selectTabBarItem("id");
Or, to add a badge (the small number on top of the button):
var num = 2;
window.uicontrols.updateTabBarItem("id", { badge: num });
Currently, the TabBar has some positioning bugs and using it with TabBar is very buggy ToolBar causes some issues.
So, to use both of them, we need do patch the PhoneGap source code.
Soon on this blog.
Useful resources
iPhone: UIControls (TabBar)
PhoneGap: TabBar Class Reference
–
Phonegap version: 0.9.0