diff --git a/ui/tab-view/tab-view.android.ts b/ui/tab-view/tab-view.android.ts index 332ec7b29..b00674ae8 100644 --- a/ui/tab-view/tab-view.android.ts +++ b/ui/tab-view/tab-view.android.ts @@ -3,6 +3,7 @@ import definition = require("ui/tab-view"); import dependencyObservable = require("ui/core/dependency-observable"); import view = require("ui/core/view"); import trace = require("trace"); +import imageSource = require("image-source"); var VIEWS_STATES = "_viewStates"; @@ -141,6 +142,7 @@ export class TabView extends common.TabView { private _tabsAddedByMe = new Array(); private _tabsCache = {}; private _androidViewId: number; + private _iconsCache = {}; constructor() { super(); @@ -358,6 +360,7 @@ export class TabView extends common.TabView { item = newItems[i]; tab = actionBar.newTab(); tab.setText(item.title); + this._setIcon(item.iconSource, tab); tab.setTabListener(this._tabListener); @@ -367,6 +370,26 @@ export class TabView extends common.TabView { } } + private _setIcon(iconSource: string, tab: android.app.ActionBar.Tab): void { + if (!iconSource) { + return; + } + + var drawable: android.graphics.drawable.BitmapDrawable; + drawable = this._iconsCache[iconSource]; + if (!drawable) { + var is = imageSource.fromFileOrResource(iconSource); + if (is) { + drawable = new android.graphics.drawable.BitmapDrawable(is.android); + this._iconsCache[iconSource] = drawable; + } + } + + if (drawable) { + tab.setIcon(drawable); + } + } + public _removeTabs(oldItems: Array) { trace.write("TabView._removeTabs(" + oldItems + ");", common.traceCategory); super._removeTabs(oldItems); diff --git a/ui/tab-view/tab-view.d.ts b/ui/tab-view/tab-view.d.ts index 2c689aa2e..0945c4f76 100644 --- a/ui/tab-view/tab-view.d.ts +++ b/ui/tab-view/tab-view.d.ts @@ -18,6 +18,11 @@ declare module "ui/tab-view" { * Gets or sets the view of the TabViewItem. */ view: view.View; + + /** + * Gets or sets the icon source of the TabViewItem. This could either be a a file name or resource id. + */ + iconSource?: string; } /** diff --git a/ui/tab-view/tab-view.ios.ts b/ui/tab-view/tab-view.ios.ts index 8e15cbca2..bc4454de1 100644 --- a/ui/tab-view/tab-view.ios.ts +++ b/ui/tab-view/tab-view.ios.ts @@ -5,6 +5,7 @@ import utilsModule = require("utils/utils"); import trace = require("trace"); import utils = require("utils/utils"); import view = require("ui/core/view"); +import imageSource = require("image-source"); // merge the exports of the common file with the exports of this file declare var exports; @@ -85,6 +86,7 @@ export class TabView extends common.TabView { private _moreNavigationControllerDelegate: UINavigationControllerDelegateImpl; private _tabBarHeight: number = 0; private _navBarHeight: number = 0; + private _iconsCache = {}; constructor() { super(); @@ -138,24 +140,28 @@ export class TabView extends common.TabView { var i: number; var length = newItems.length; - var newItem: definition.TabViewItem; + var item: definition.TabViewItem; var newControllers: NSMutableArray = NSMutableArray.alloc().initWithCapacity(length); var newController: UIViewController; for (i = 0; i < length; i++) { - newItem = newItems[i]; + item = newItems[i]; - this._addView(newItem.view); + this._addView(item.view); - if (newItem.view.ios instanceof UIViewController) { - newController = newItem.view.ios; + if (item.view.ios instanceof UIViewController) { + newController = item.view.ios; } else { newController = new UIViewController(); - newController.view.addSubview(newItem.view.ios); + newController.view.addSubview(item.view.ios); + } + + var icon = this._getIcon(item.iconSource); + newController.tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(item.title, icon, i); + if (!icon) { + newController.tabBarItem.setTitlePositionAdjustment({ horizontal: 0, vertical: -20 }); } - newController.tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(newItem.title, null, -1); - newController.tabBarItem.setTitlePositionAdjustment({ horizontal: 0, vertical: -20 }); newControllers.addObject(newController); } @@ -166,6 +172,25 @@ export class TabView extends common.TabView { this._ios.moreNavigationController.delegate = this._moreNavigationControllerDelegate; } + private _getIcon(iconSource: string): UIImage { + if (!iconSource) { + return null; + } + + var image: UIImage; + image = this._iconsCache[iconSource]; + if (!image) { + var is = imageSource.fromFileOrResource(iconSource); + if (is && is.ios) { + is.ios.renderingMode = UIImageRenderingMode.UIImageRenderingModeAlwaysOriginal; + this._iconsCache[iconSource] = is.ios; + image = is.ios; + } + } + + return image; + } + public _onSelectedIndexPropertyChangedSetNativeValue(data: dependencyObservable.PropertyChangeData) { super._onSelectedIndexPropertyChangedSetNativeValue(data);