public
Last active

Custom Navigation Controller, compatible with Android and iPhone

  • Download Gist
customnav.js
JavaScript
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
// Flags, is this for Android or iPhone?
var iPhone = false;
var Android = false;
if(Ti.Platform.osname == 'iphone'){
iPhone = true
};
if(Ti.Platform.osname == 'android'){
Android = true
};
 
// our navigation function, to be compatible with both platforms
function NavigationController(a){
// this is to avoid errors
a = a || {};
a.window = a.window || Ti.UI.createWindow();
// this is to handle the iPhone Nav functionality
if(iPhone){
var win = Ti.UI.createWindow();
var nav = Ti.UI.iPhone.createNavigationGroup({
window: a.window
});
win.add(nav);
win.push = function(b){
nav.open(b);
};
return win;
}
// there is no Nav in Android, so let's return the window
if(Android){
a.window.push = function(b){
b.open({
fullscreen:false
});
}
return a.window;
}
}
 
// start the app right here
var win = Ti.UI.createWindow({
backgroundColor:'green'
});
 
var btn = Ti.UI.createButton({
title:'push next window to the stack',
width:300,
height:50
});
 
win.add(btn);
 
// let's call our custom navigation function
var navigation = NavigationController({
window:win
});
// open it
navigation.open();
 
// example of how to push (or open) the next window
btn.addEventListener('click', function(){
var win2 = Ti.UI.createWindow({
backgroundColor:'red'
});
navigation.push(win2);
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.