Appcelerator 中的自定义选项卡栏 - 如何返回到根窗口
Custom Tab Bar in Appcelerator - How to return to root window?
我有一个错误,需要在Appcelerator Titanium上开发的iOS应用程序中修复。
我一直在使用customTabBar(在GitHub上可用)来创建定制的标签栏,它工作得很好!
唯一的小问题是它删除了在图标上选项卡并返回到根窗口的选项(就像在iOS中适当的本机tabBar一样)。
因此,如果我在应用程序中向下钻取 3 或 4 个窗口,点击选项卡图标没有任何作用,我必须通过多次向后点击来导航回开头。
这是我正在使用的完整自定义选项卡栏.js脚本:
CustomTabBar = function(settings) {
var tabBarItems = [];
var tabCurrent = 2;
var resetTabs = function() {
for(var i = 0; i < tabBarItems.length; i++) {
// Clear all the images to make sure only
// one is shown as selected
tabBarItems[i].image = tabBarItems[i].backgroundImage;
}
};
var assignClick = function(tabItem) {
tabItem.addEventListener('click', function(e) {
// Just fetching the 'i' variable from the loop
var pos = e.source.pos;
if (tabCurrent == pos) {
// TODO
// Change back to root window, like the native tab action.
// code must go in here
return false;
}
// Switch to the tab associated with the image pressed
settings.tabBar.tabs[pos].active = true;
tabCurrent = pos;
// Reset all the tab images
resetTabs();
// Set the current tab as selected
tabBarItems[pos].image = settings.imagePath + settings.items[pos].selected;
});
};
// Create the container for our tab items
var customTabBar = Ti.UI.createWindow({
height: 48,
backgroundImage:'images/tabbarbackground.png',
bottom: 0
});
for(var i = 0; i < settings.items.length; i++) {
// Go through each item and create an imageView
tabBarItems[i] = Titanium.UI.createImageView({
// background is the default image
backgroundImage: settings.imagePath + settings.items[i].image,
width: settings.width,
height: settings.height,
left: settings.width * i
});
// Pass the item number (used later for changing tabs)
tabBarItems[i].pos = i;
assignClick(tabBarItems[i]);
// Add to the container window
customTabBar.add(tabBarItems[i]);
}
// Display the container and it's items
customTabBar.open();
// Set the first item as current :)
resetTabs();
//tabBarItems[0].image = settings.imagePath + settings.items[0].selected;
tabBarItems[2].image = settings.imagePath + settings.items[2].selected;
return {
hide: function() { customTabBar.hide(); },
show: function() { customTabBar.show(); }
};
};
包含我需要添加的位的函数已经标记,但只是空。在这里:
var assignClick = function(tabItem) {
tabItem.addEventListener('click', function(e) {
// Just fetching the 'i' variable from the loop
var pos = e.source.pos;
if (tabCurrent == pos) {
// TODO
// Change back to root window, like the native tab action.
// code must go in here
return false;
}
// Switch to the tab associated with the image pressed
settings.tabBar.tabs[pos].active = true;
tabCurrent = pos;
// Reset all the tab images
resetTabs();
// Set the current tab as selected
tabBarItems[pos].image = settings.imagePath + settings.items[pos].selected;
});
};
customTabBar 在现有选项卡栏上放置一个窗口(实际上,只是一个视图)。然后,它会处理通过的点击。但是,您必须处理单击事件才能在选项卡之间切换,并且如您所指出的,跟踪堆栈上的所有窗口。
但你知道吗?你太辛苦了。该平台已经为您完成了所有这些工作。
传递点击事件(通过禁用对叠加层的触摸),底层选项卡组将发挥自己的魔力。然后,您需要做的就是使用选项卡组的焦点事件更新 UI(evt.index 是焦点选项卡,evt.previousIndex 是模糊的)。
应用.js:
Ti.include('overrideTabs.js');
/*
This is a typical new project -- a tab group with three tabs.
*/
var tabGroup = Ti.UI.createTabGroup();
/*
Tab 1.
*/
var win1 = Ti.UI.createWindow({ title: 'Tab 1', backgroundColor: '#fff' });
var tab1 = Ti.UI.createTab({
backgroundImage: 'appicon.png',
window: win1
});
var button1 = Ti.UI.createButton({
title: 'Open Sub Window',
width: 200, height: 40
});
button1.addEventListener('click', function (evt) {
tab1.open(Ti.UI.createWindow({ title: 'Tab 1 Sub Window', backgroundColor: '#fff' }));
});
win1.add(button1);
tabGroup.addTab(tab1);
/*
Tab 2.
*/
tabGroup.addTab(Ti.UI.createTab({
backgroundImage: 'appicon.jpg',
window: Ti.UI.createWindow({ title: 'Tab 2', backgroundColor: '#fff' })
}));
/*
Tab 3.
*/
tabGroup.addTab(Ti.UI.createTab({
backgroundImage: 'appicon.png',
window: Ti.UI.createWindow({ title: 'Tab 3', backgroundColor: '#fff' })
}));
/*
Now call the overrideTabs function, and we're done!
*/
overrideTabs(
tabGroup, // The tab group
{ backgroundColor: '#f00' }, // View parameters for the background
{ backgroundColor: '#999', color: '#000', style: 0 }, // View parameters for selected tabs
{ backgroundColor: '#333', color: '#888', style: 0 } // View parameters for deselected tabs
);
tabGroup.open();
overrideTabs.js:
/**
* Override a tab group's tab bar on iOS.
*
* NOTE: Call this function on a tabGroup AFTER you have added all of your tabs to it! We'll look at the tabs that exist
* to generate the overriding tab bar view. If your tabs change, call this function again and we'll update the display.
*
* @param tabGroup The tab group to override
* @param backgroundOptions The options for the background view; use properties like backgroundColor, or backgroundImage.
* @param selectedOptions The options for a selected tab button.
* @param deselectedOptions The options for a deselected tab button.
*/
function overrideTabs(tabGroup, backgroundOptions, selectedOptions, deselectedOptions) {
// a bunch of our options need to default to 0 for everything to position correctly; we'll do it en mass:
deselectedOptions.top = deselectedOptions.bottom
= selectedOptions.top = selectedOptions.bottom
= backgroundOptions.left = backgroundOptions.right = backgroundOptions.bottom = 0;
// create the overriding tab bar using the passed in background options
backgroundOptions.height = 50;
var background = Ti.UI.createView(backgroundOptions);
// pass all touch events through to the tabs beneath our background
background.touchEnabled = false;
// create our individual tab buttons
var increment = 100 / tabGroup.tabs.length;
deselectedOptions.width = selectedOptions.width = increment + '%';
for (var i = 0, l = tabGroup.tabs.length; i < l; i++) {
var tab = tabGroup.tabs[i];
// position our views over the tab.
selectedOptions.left = deselectedOptions.left = increment * i + '%';
// customize the selected and deselected based on properties in the tab.
selectedOptions.title = deselectedOptions.title = tab.title;
if (tab.backgroundImage) {
selectedOptions.backgroundImage = deselectedOptions.backgroundImage = tab.backgroundImage;
}
if (tab.selectedBackgroundImage) {
selectedOptions.backgroundImage = tab.selectedBackgroundImage;
}
if (tab.deselectedBackgroundImage) {
deselectedOptions.backgroundImage = tab.deselectedBackgroundImage;
}
selectedOptions.visible = false;
background.add(tab.deselected = Ti.UI.createButton(deselectedOptions));
background.add(tab.selected = Ti.UI.createButton(selectedOptions));
}
// update the tab group, removing any old overrides
if (tabGroup.overrideTabs) {
tabGroup.remove(tabGroup.overrideTabs);
}
else {
tabGroup.addEventListener('focus', overrideFocusTab);
}
tabGroup.add(background);
tabGroup.overrideTabs = background;
}
function overrideFocusTab(evt) {
if (evt.previousIndex >= 0) {
evt.source.tabs[evt.previousIndex].selected.visible = false;
}
evt.tab.selected.visible = true;
}
https://gist.github.com/853935
相关文章:
- 当我编码窗口时如何返回真或假.确认自己
- 动态弹出窗口'通过调用一个永远运行并返回's每个周期后的数据
- 窗口对象属性返回一个dom节点
- 将数据从弹出窗口返回到父窗口(SMARTY)
- 黑莓网站:窗口大小(窗口高度)在黑莓模拟器中返回不正确的值
- 为什么根据括号的位置来引用窗口或对象?并返回undefined
- 根据弹出窗口javascript的返回值更改asp.net面板的可见性
- 使用Javascript将拖动的小程序返回到浏览器窗口
- (窗口中的'ontouchstart')返回true,但没有触摸事件
- 使用HTML搜索,需要JavaScript来搜索数组并在新窗口中返回结果
- Appcelerator 中的自定义选项卡栏 - 如何返回到根窗口
- 根据在弹出窗口中单击的按钮返回 true 或 false
- JQuery 打开弹出窗口并从单击的链接返回值
- 是否可以使控制器返回模式窗口的数据?MVC5 ASP.NET
- 这将返回窗口对象
- Window.open没有't返回窗口参考IE9
- 为什么构造函数(带“return this”)返回窗口对象?
- 为什么元素的相对定位有时会返回窗口(0,0)的顶部,而在其他地方会正确返回
- 为什么这个立即调用的方法返回窗口对象
- jQuery 返回窗口对象