import { layout , View , Color } from 'tns-core-modules/ui/core/view/view' ; import { BottomBarBase , inactiveTintColor , activeTintColor , barBackgroundColor , items } from '../bottombar.base' ; import { BottomBarItem } from '../../bottombar-item/ios/bottombar-item' ; import { imageConverter } from '../../utils/utils.common' ; export class BottomBarControllerDelegate extends NSObject implements UITabBarControllerDelegate { public static ObjCProtocols = [ UITabBarControllerDelegate ] ; private _owner : WeakRef < BottomBar > ; public static initWithOwner ( owner ) { const delegate = < BottomBarControllerDelegate > BottomBarControllerDelegate . new ( ) ; delegate . _owner = owner ; return delegate ; } tabBarControllerDidSelectViewController ( tabBarController , viewController ) { const owner = this . _owner . get ( ) ; owner . onTabSelected ( tabBarController . selectedIndex ) ; } } export class BottomBar extends BottomBarBase { public _tabBarController : UITabBarController ; private _delegate : BottomBarControllerDelegate ; _items : BottomBarItem [ ] ; nativeView : UITabBar ; get ios ( ) : UITabBar { return this . nativeView ; } public createNativeView ( ) { this . _tabBarController = UITabBarController . new ( ) ; this . setViewControllers ( ) ; const actualHeight = this . getActualSize ( ) . height ; this . height = actualHeight ? actualHeight : 49 ; this . _tabBarController . tabBar . barTintColor = this . barBackgroundColor . ios ; this . _tabBarController . tabBar . unselectedItemTintColor = this . inactiveTintColor . ios ; this . _tabBarController . tabBar . tintColor = this . activeTintColor . ios ; return this . _tabBarController . tabBar ; } _addChildFromBuilder ( name , value ) { if ( name === 'BottomBarItem' ) { this . _items . push ( this . createItem ( value , this . _items . length ) ) ; } } public initNativeView ( ) { super . initNativeView ( ) ; this . _delegate = this . _tabBarController . delegate = BottomBarControllerDelegate . initWithOwner ( new WeakRef ( this ) ) ; } private setViewControllers ( ) { this . _tabBarController . viewControllers = ( NSArray as any ) . arrayWithArray ( this . _items . map ( item => item . viewController ) ) ; } private createItem ( value , id ) { const itemViewController = this . createItemViewController ( value , id ) ; value . viewController = itemViewController ; value . setNativeView ( itemViewController . tabBarItem ) ; return value ; } private createItemViewController ( item , id ) { const itemViewController = UIViewController . new ( ) ; itemViewController . tabBarItem = UITabBarItem . new ( ) ; item . index = id ; if ( item . _icon && item . _title ) { itemViewController . tabBarItem = itemViewController . tabBarItem . initWithTitleImageTag ( item . _title , imageConverter ( item . _icon ) . ios , item . index ) ; if ( item . _checkedIcon ) { itemViewController . tabBarItem . selectedImage = imageConverter ( item . _checkedIcon ) . ios ; } if ( item . _badge ) { itemViewController . tabBarItem . badgeValue = item . _badge ; } } else { itemViewController . tabBarItem . init ( ) ; itemViewController . tabBarItem . tag = id ; } return itemViewController ; } public onMeasure ( widthMeasureSpec , heightMeasureSpec ) { const width = layout . getMeasureSpecSize ( widthMeasureSpec ) ; const widthMode = layout . getMeasureSpecMode ( widthMeasureSpec ) ; const height = layout . getMeasureSpecSize ( heightMeasureSpec ) ; const heightMode = layout . getMeasureSpecMode ( heightMeasureSpec ) ; const widthAndState = View . resolveSizeAndState ( width , width , widthMode , 0 ) ; const heightAndState = View . resolveSizeAndState ( height , height , heightMode , 0 ) ; this . setMeasuredDimension ( widthAndState , heightAndState ) ; } [ items . setNative ] ( items ) { if ( items ) { this . _items = [ ... items ] ; if ( this . _tabBarController . viewControllers . count > 0 ) { this . _tabBarController . viewControllers = null ; } this . _items . forEach ( ( item , index ) => { this . createItem ( item , index ) ; } ) ; this . setViewControllers ( ) ; } } [ inactiveTintColor . setNative ] ( color ) { this . nativeView . unselectedItemTintColor = color . ios ; } [ activeTintColor . setNative ] ( color ) { this . nativeView . tintColor = color . ios ; } [ barBackgroundColor . setNative ] ( color ) { this . nativeView . barTintColor = color . ios ; } selectItem ( index ) { this . _tabBarController . selectedIndex = index ; } }