Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created December 21, 2011 04:29
Show Gist options
  • Select an option

  • Save jonalter/1504589 to your computer and use it in GitHub Desktop.

Select an option

Save jonalter/1504589 to your computer and use it in GitHub Desktop.
iOS: custom TabbedBar
var win = Ti.UI.createWindow();
var view = Ti.UI.createView({
top:0,
left:0,
backgroundImage:'bluebar.png',
height:44,
width:320
});
var tb = createTabbedBar({
labels: ['This','is a','test'],
height: 30,
width: 250
});
tb.addEventListener('click', function(e){
Ti.API.info('Clicked button '+e.source.index);
Ti.API.info(JSON.stringify(e));
});
view.add(tb);
win.add(view);
win.open();
function createTabbedBar(params){
var params = params || {};
var width = params.width || 200;
var height = params.height || 25;
var labels = params.labels || ['default','labels'];
var buttons = [];
var mainView = Ti.UI.createView({
width: width,
height: height,
layout: 'horizontal'
});
var buttonWidth = Math.floor(width/labels.length);
for(var i = 0, j = labels.length; i < j; i++){
buttons[i] = createButtons({
title: labels[i],
height: height,
width: buttonWidth,
index: i,
numberOfButtons: j
});
buttons[i].addEventListener('click', function(e){
for(var k = 0, l = buttons.length; k < l; k++){
if(e.source.index == k){
buttons[k].backgroundImage = buttons[k].selectedImage;
}else{
buttons[k].backgroundImage = buttons[k].normalImage;
}
}
})
mainView.add(buttons[i]);
}
function createButtons(args){
var view = Ti.UI.createView(args);
var label = Ti.UI.createLabel({
text: args.title,
color: '#FFF',
textAlign: 'center',
index: args.index,
font: {fontSize:14,fontWeight:'normal'}
});
if(args.index == 0){
view.normalImage = 'leftButtonN.png';
view.selectedImage = 'leftButtonP.png';
view.backgroundImage = view.selectedImage;
}else if(args.index == (args.numberOfButtons-1) ){
view.normalImage = 'rightButtonN.png';
view.selectedImage = 'rightButtonP.png';
view.backgroundImage = view.normalImage;
}else{
view.normalImage = 'normal.png';
view.selectedImage = 'selected.png';
view.backgroundImage = view.normalImage;
}
view.add(label);
return view
};
return mainView;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment