Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabBarIOS on React Native not working as expected

Tags:

react-native

I'm having problems on implementing Tab Bar for React Native. The documentation does not exist (http://facebook.github.io/react-native/docs/tabbarios.html) and the example at their front page is insufficient (e.g. missing required property icon).

I managed to implement it from code point-of-view and something showed up. But only a light blue box taking half of the space on the screen.

My "working" code looked like this:

<TabBarIOS>
  <TabBarIOS.Item title="Wooden" selected={false} icon={require('image!wooden')}>
    <NavigatorIOS initialRoute={{ title: 'Wooden' }} />
  </TabBarIOS.Item>
</TabBarIOS>

But like said, the end result was not expected.

Has anyone managed to implement TabBarIOS successfully? I tried to search through source code but there were no gotchas that would've helped me.

like image 903
Samuli Hakoniemi Avatar asked Mar 28 '15 21:03

Samuli Hakoniemi


2 Answers

Answering to my own question, this is how I got it working:

First we need to define TabBarItemIOS:

var React = require('react-native');
var {
  Image,
  StyleSheet,
  Text,
  View,
  TabBarIOS
} = React;

var TabBarItemIOS = TabBarIOS.Item;

Then, we can use a helper for defining icons:

function _icon(imageUri) {
  return {
    uri: imageUri,
    isStatic: true
  };
}

And, well... the rest of the actual code:

<TabBarIOS>
  <TabBarItemIOS
    icon={_icon('favorites')}>
  </TabBarItemIOS>
  <TabBarItemIOS
    icon={_icon('history')}>
  </TabBarItemIOS>
  <TabBarItemIOS
    icon={_icon('more')}>
  </TabBarItemIOS>
</TabBarIOS>

This returns at least basic kind of TabBar.

This is based on the example which can be found from here: https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/TabBarExample.js

like image 131
Samuli Hakoniemi Avatar answered Sep 22 '22 07:09

Samuli Hakoniemi


I think you were already on the right track with your first post. You need to use the right resolutions for your icons. More here: Apple iOS Docs There needs to be 3 resolutions for the same icon, e.g 32x32 = @1, 64x64 = @2 and 92x92 = @3.

Remember to follow the instructions how to create image assets in the React Native Docs for Static Resources. One image resource needs to have the same name as the image asset in Xcode.

And maybe your image doesn't have transparent borders like this case.

Here is my working code:

...
var homeIconUnactive = require('image!home-unactive');
var homeIconActive = require('image!home-active');
...

<TabBarIOS.Item
    title='Home'
    selected={this.state.selectedTab === 'EventList'}
    icon={homeIconUnactive}
    selectedIcon={homeIconActive}
    onPress={() => {
        this.setState({
            selectedTab: 'EventList',
        });
    }}>
    <EventList/>
</TabBarIOS.Item>

My tab icons are still blue when they are selected though...

like image 30
Chau Thai Avatar answered Sep 21 '22 07:09

Chau Thai