×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2012.821 | Released: 23 May 2012, 2:01am | What's New | Download Now

widget.newTabBar( )

Description:

This widget requires Corona build 2011.646 or higher.

Corona SDK tabBar widget

Creates a customizable bar with tab buttons. Buttons will be auto-positioned, depending on how many buttons you add to the tabBar. There is no limit to how many buttons you can add technically, but you want to avoid cramming too many buttons on any one tabBar (depending on the width of your target device's screen).

Syntax:

widget.newTabBar( [options] )

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    local widget = require "widget"
 
    local function onBtnPress( event )
        print( "You pressed tab button: " .. event.target.id )
    end
 
    local tabButtons = {
        {
            label="First Tab",
            up="firstIcon.png",
            down="firstIcon-down.png",
            width=32, height=32,
            onPress=onBtnPress,
            selected=true
        },
        {
            label="Second Tab",
            up="secondIcon.png",
            down="secondIcon-down.png",
            width=32, height=32,
            onPress=onBtnPress
        },
    }
    
    local tabs = widget.newTabBar{
        top=430,
        buttons=tabButtons
    }

Parameters:

widget.newTabBar() takes a single argument, options, which is a Lua table that accepts the following parameters:

 
width, height
numbers. Set these if you want to use a custom width/height for your tab bar. The default width is equal to display.contentWidth, and the default height is 50.

 
left, top
numbers. This is the position where you want your tab bar to be created. Most iOS apps place the tabBar at the bottom of the screen (which would be display.contentHeight - tabBar.height, or default 50). The default values for left and top are 0, 0.

 
background
string. If you'd rather use a static image for the background of your tab bar, specify the filename to your graphics resource here.

 
baseDir
system folder. If using a background image and need to specify a base directory different from the default (system.ResourceDirectory), you may set this to system.DocumentsDirectory, system.TemporaryDirectory, or system.CachesDirectory).

 
topGradient
gradient object via graphics.newGradient(). If no graphics asset is specified with the 'background' parameter, by default the tabBar background will consist of a gradient for the top half of the background, and a solid color for the bottom. If you want to use a custom gradient for the top-half of the tabBar, create one using graphics.newGradient() and set a reference to it here.

 
bottomFill
table. If no custom graphic is being used, you can optionally set the color of the bottom half of the tabBar. Pass the red, green, blue, and alpha channels in a table. The default is black:
{ 0, 0, 0, 255 }

 
buttons
table. This is a table that will hold all parameters and options for the individual tab buttons. Please see the Buttons Table section below for more information.

 

Buttons Table

The buttons table is a parameter that is referenced as an option for the tabBar widget. This table should have sub-tables that represent the individual buttons that will be placed on the tabBar.

In the example shown above, tabButtons is the buttons table that has two buttons (see it for further demonstration).

Button Parameters

You can set the following parameters for each button table:

 
id
string. You can set this to be whatever you want. By default this will equal the number that represents the button's index on the tabBar. For instance, the second button would have a default id of 2.

 
up
string. This represents the filename for the 'up'-state icon for this button. This should not be omitted.

 
upWidth, upHeight
[NOTE: Deprecated as of build 2012.721—use width/height instead] numbers. These parameters should be equal to the width and height of your 'up'-state icon for the button. Recommended icon size is 32 x 32.

 
down
string. This represents the filename for the 'down'-state (selected tab) icon for this button. This should not be omitted.

 
downWidth, downHeight
[NOTE: Deprecated as of build 2012.721—use width/height instead]numbers. These parameters should be equal to the width and height of your 'down'-state (selected tab) icon for the button. Recommended icon size is 32 x 32.

 
width, height
numbers. Requires build 2012.721 or higher (and should be used in place of upWidth/upHeight and downWidth/downHeight. These numbers should represent the width and height of the tab button image size. Default is 32 x 32.

 
baseDir
system directory. If the icon graphics for your button are not in system.ResourceDirectory (default), specify the alternate base directory here.

 
label
string. This is the text that will appear below the button's icon. If no label is specified, no text will be shown, and the icon will be shifted down. It is recommended you remain consistent with all buttons on your tab bar (if you set a label for one, you should set one for all).

 
font
string. The font that will be used for your label text, if label text is specified. Default is native.systemFontBold.

 
size
number. The font size (in pixels) for your label text. Default is 10 (in most cases, this should be very small).

 
labelColor
table. This is a table that will hold values that represent the red, green, blue, and alpha channels for the color of your label text. The default is:
{ 124, 124, 124, 255 }

 
cornerRadius
number. When a tab button is selected, a rounded rectangle appears over the button. This parameter sets the roundness of the corners of that rectangle. The default is 4.

 
selected
boolean (true/false). This determines if the button is in it's 'selected' (down) state upon creation. No more than ONE of your buttons should have this parameter set to true when you create your buttons table.

 
onPress
function. This is the listener function that is called when the tab button is pressed.

 

Event Table

Your onPress event listener can have up to one argument, which will be an event table that holds specific event-related data, which includes:

 
event.name
This will always be "tabButtonPress"

 
event.target
This is a reference to the actual tab button that triggered the event.

 
event.targetParent
This is a reference to the tabBar widget which is the parent of the button that triggered the event.

 

Properties and Methods:

Below are the individual properties and methods associated with the tabBar widget (not the buttons):

 
pressButton( buttonIndex [, invokeListener] )
Available since build 2011.700. If you need to make a specific button on the tab bar pressed, you can call the pressButton() method and specify the button index you want to be in the "down" state. Optionally, you can invoke the 'onPress' listener for the button at this time (default is false). Example:

    myTabBar:pressButton( 1 )  -- put first button in 'down' state
 
    myTabBar:pressButton( 3, true )  -- 3rd button in down state and invoke onPress listener

 
view
[NOTE: Deprecated as of build 2012.721—the widget is now the display object]

If you need to do something with the widget's display object (such as place the button into a display group), you need to do so using the object's .view property. Example:

    -- prior to build 2012.721:   
 
    -- WRONG, will produce error:
    myGroup:insert( myTabBar )
 
    -- CORRECT: access display group with .view property
    myGroup:insert( myTabBar.view )
 
    - after build 2012.72:
 
    myGroup:insert( myTabBar ) -- correct

 
x, y
numbers. Normally, to access display object properties of the widget, you'd do so by using the .view property of the widget. However, you can optionally use the x/y property shortcut to change the on-screen location of the widget. Example:

    -- move the tabBar to specific location on screen:
    myTabBar.x = 160
    myTabBar.y = 480-25

Removing the widget:

This widget can be removed in the same manner as any other display object, by using display.remove() or the removeSelf() method. Example:

    display.remove( myTabBar )
    myTabBar = nil
 
    -- or...
 
    myTabBar:removeSelf()
    myTabBar = nil
 
    -- IMPORTANT NOTE:
    -- Don't forget to set the reference variable to nil! (as shown above)

Returns:

Corona widget object.

Remarks:
Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    2011.646
  • Windows:
    2011.646
  • iOS:
    2011.646
  • Android:
    2011.646

Replies

ckausik
User offline. Last seen 11 hours 19 min ago. Offline
Joined: 18 Jan 2011

Is newTabBar available for build 645? I am getting the following error - "attempt to call global 'newTabBar' (a nil value)".

jonathanbeebe's picture
jonathanbeebe
User offline. Last seen 1 day 7 hours ago. Offline
Staff
Joined: 12 Apr 2011

@ckausik: It'll be in the Daily Build that comes after 645 ... the documentation will be updated to reflect that once that build is available.

alarew
User offline. Last seen 3 weeks 23 hours ago. Offline
Joined: 10 Feb 2010

I cant seem to get backGround = "image.png" to work in the newTabBar widget. Getting nothing. No effect. And no errors either that I can see. I have the latest Daily Build 671.

sunmils
User offline. Last seen 1 week 3 days ago. Offline
Joined: 18 Apr 2011

Is there a tutorial, how to make the gradient of the Icon with colors etc? Cant find something, and i'm a real CG noob :) Thanks

jonathanbeebe's picture
jonathanbeebe
User offline. Last seen 1 day 7 hours ago. Offline
Staff
Joined: 12 Apr 2011

@alarew: The background parameter was broken, but is now fixed as of build 2011.700. As a reminder, it's background (all lowercase).

dmekersa's picture
dmekersa
User offline. Last seen 6 days 17 hours ago. Offline
Joined: 21 Jul 2011

Advice:
The function called when a button is pressed is not blocking the event (even if I return true), and the event is catch by any display object behind the tab bar.

dmekersa's picture
dmekersa
User offline. Last seen 6 days 17 hours ago. Offline
Joined: 21 Jul 2011

In build 695 the pressButton method is not working:
attempt to call method 'pressButton' (a nil value)

jonathanbeebe's picture
jonathanbeebe
User offline. Last seen 1 day 7 hours ago. Offline
Staff
Joined: 12 Apr 2011

@dmekersa: I will look into the 'non-blocking' tab button issue (in the future, to prevent bugs from being over-looked, please submit a bug report—thanks!).

About the pressButton() event, I should've clarified that in the documentation, but that was not available until build 700. I've updated the documentation to state that.

UPDATE: The non-blocking issue has been fixed and should appear in the next daily build (after 2011.701).

dmekersa's picture
dmekersa
User offline. Last seen 6 days 17 hours ago. Offline
Joined: 21 Jul 2011

Thanks Jonathan!
I wanted to submit a bug, but it needs to provide a project sample, and I didn't have time for that at the moment. Sorry. Next time I will.

jeff.wesson
User is online Online
Joined: 26 Oct 2011

Can you tell me what

demoTabs:pressButton( 2 , true)

has changed to since build 721 ? as it no longer works for me.

jeff.wesson
User is online Online
Joined: 26 Oct 2011

Thanks, now working in build 722.

eduardo.pinheiro
User offline. Last seen 17 weeks 2 days ago. Offline
Joined: 10 Jan 2012

[comment deleted]

fpierron
User is online Online
Joined: 8 Mar 2010

RESOLVED : I forgot the / in the path... sorry

hi,
the tab bar sticks at the top :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local path = "ui_MainNav"
local tabButtons = {
        {
                id = "now",
                up = path.."now_up@2.png",
                down = path.."now_down@2.png",
                onRelease = onButtonEvent
        },
        {
                id = "rock",
                up = path.."rock_up@2.png",
                down = path.."rock_down@2.png",
                onRelease = onButtonEvent
        },
        {
                id = "yellow",
                up = path.."yellow_up@2.png",
                down = path.."yellow_down@2.png",
                onRelease = onButtonEvent
        },      
}
 
local h = display.contentHeight-96
local tabBar = widget.newTabBar {
        height = 96,
        top = h,
        background = "bg_nave_footer.png",
        buttons = tabButtons
        
}

jesse.manuel
User offline. Last seen 9 hours 55 min ago. Offline
Joined: 6 Oct 2011

It looks like since Build 722, the tab-bar errors out (only on the device) when the 'label' parameter is specified. Any ideas?

I see this in the console..
: Lua Runtime Error: lua_pcall failed with status: 2, error message is: ?:0: attempt to perform arithmetic on a userdata value

staypuffinpc
User offline. Last seen 4 days 23 hours ago. Offline
Joined: 18 Jan 2011

The tabBar does not behave the way indicated in this documentation. When I use the Storyboard demo that comes in the sample files and attempt to capture the event of the pressed buttons, event.target does not return the pressed button. Furthermore, event.targetParent returns nil. Thus, the only way I was able to get at properties of the clicked button was to reference the indexed button in the button table, like so

1
storyboard.gotoScene(tabButtons[event.target.id].scene)

Additionally, I had to make the tabButtons table global in order to access this data. What am I missing?

chevol
User offline. Last seen 16 hours 29 min ago. Offline
Joined: 10 May 2011

Is there a way to remove the overlay when a button is focused? Its that semi transparent glossy layer that appears on the active button. Can we remove that somehow?

JonPM
User offline. Last seen 4 weeks 2 days ago. Offline
Joined: 5 Mar 2012

I am using a custom font for my tab bar (which shows up fine), however when I indicate size it does not change. Any idea why? even if I put size = 30 and there is not change whatsoever.

Tom
User offline. Last seen 11 hours 14 min ago. Offline
Staff
Joined: 13 Jul 2010

The comment section for this API has been closed. Please continue the discussion on the Corona forums.
http://developer.anscamobile.com/forum/2012/04/02/widgetnewtabbar