mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #216 from NativeScript/tab-view-images
Added functionality to specify images on TabView tabs.
This commit is contained in:
@ -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<android.app.ActionBar.Tab>();
|
||||
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<definition.TabViewItem>) {
|
||||
trace.write("TabView._removeTabs(" + oldItems + ");", common.traceCategory);
|
||||
super._removeTabs(oldItems);
|
||||
|
5
ui/tab-view/tab-view.d.ts
vendored
5
ui/tab-view/tab-view.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 = <UIViewController>newItem.view.ios;
|
||||
if (item.view.ios instanceof UIViewController) {
|
||||
newController = <UIViewController>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);
|
||||
|
||||
|
Reference in New Issue
Block a user