react native TabBarIOS.Item - react-native

I am using TabBarIOS.Item which has three options that each lead to a NavigatorIOS, I want to use the TabBar's so that when you click them it takes you to the first page of the NavigatorIOS as apposed to the last one before the user changed tabs, is that possible?
Thanks, Adam

So the answer is, is that TabBARIOS.item is an object and navigatorIOS is also an object, so you can give them both a ref. so they look like this.
<TabBarIOS.Item
title="partners"
selected={this.state.selectedTab === "Partners"}
icon={require("./App/assets/partnersIcon.png")}
onPress={this.partnersHandleChange.bind(this)} >
<View style={styles.main}>
<NavigatePartners ref="partners"></NavigatePartners>
</View>
</TabBarIOS.Item>
And the navigator looks like this
turn (
<NavigatorIOS
ref="navigator"
style={styles.mainContainer}
initialRoute={{
title: 'Partners',
component: Partners,
backButtonTitle: 'Back',
}}/>
Then you change the onclick to look like this
partnersHandleChange(){
if (this.state.selectedTab == "Partners")
{
this.refs.partners.refs.navigator.popToTop();
}
this.setState({
selectedTab: 'Partners',
})
};

Related

How to navigate to other screen using the headerRight on the navigator code?

The title is very confusing, but the explanation that I will give will be more clear.
I am creating one StackNavigator for my app, and I am defining one icon to be displayed on the header of one of my screens like so:
const navigator= createStackNavigator(
{
Initial: {
screen: Posts,
navigationOptions: {
title: "All Posts",
headerRight: () => {
return (
<TouchableOpacity style={{ padding: 5 }}>
<Feather name={"plus"} size={30} />
</TouchableOpacity>
);
},
},
},
Details: Details,
Create: Create,
},
{
initialRouteName: "Initial",
}
);
const App = createAppContainer(navigator);
export default () => {
return (
<Provider>
<App />
</Provider>
);
};
The problem is that I want to navigate the user to the Create screen when the user presses the icon that I am rendering on the right side of the header, but I do not know how to have access to the navigation.navigate() function that the navigator generates for all the screens. I know that I can define the header on the Posts' screen file, so I have access to the navigation.navigate() function, but I want to know if there is some way to do it on the App.js file.
EDIT
Reading the documentation I saw that the way that I am was creating the navigator is not what the official documentation recommends. I learned to make it like this by one old course, using React Navigation 4.x, and now with React Navigation 6.x I perceived the difference in the creation and changed the way that I was doing it on my app. You can check the documentation for the problem that I was having here
You can prepare your navigation option this way by sending props
options={(props) => ({
headerRight: () => <TouchableOpacity
onPress={()=>props.navigation.navigate('Screen')} style={{ padding: 5 }}>
<Feather name={"plus"} size={30} />
</TouchableOpacity>
})}

Hide navigation bar in NavigatorIOS component (react-native) doesn't work after RN update

I had recently upgraded my react native project from ~0.28 to the most recent version (0.43.2) and for some reason my navigation bar no longer hides for me.
Here is the code (it is sitting in a TabBarIOS component):
<TabBarIOS.Item
selected={this.state.selectedTab === 'home'}
title='Home'
icon={require ('./Icons/IconImages/HomeTabIcon.png')}
onPress={
() => this._tabPressed('home')
}>
<NavigatorIOS
style={styles.container}
ref="nav"
interactivePopGestureEnabled={false}
initialRoute={{
title: 'Home',
component: HomeNavigationController,
navigationBarHidden: true, //this does nothing now
showTabBar: false, //this is to hide the bottom tabBar
passProps: {
...
},
}}/>
</TabBarIOS.Item>
Adding it outside initialRoute also does not work:
<NavigatorIOS
style={styles.container}
ref="nav"
interactivePopGestureEnabled={false}
initialRoute={{
title: 'Home',
component: HomeNavigationController,
showTabBar: false,
passProps: {...},
}}
navigationBarHidden={true} // does not work
/>
So, after trying to isolate the problem by stripping out everything and reducing it to its most basic form, I realized that it the problem wasn't in any code that I could find.
I initialized a new project from scratch and then re-added all my components and now it works just fine.

React Native NavigatorIOS won't re-render Component

I have a TabBar with multiple TabBar.Item components. Each TabBar.Item component has it's own NavigatorIOS.
Here's an example of my code for my TabBar.js
<TabBarIOS>
<TabBarIOS.Item
selected={this.state.selectedTab === "profile"}
systemIcon={"most-viewed"}
onPress={() => this.setTab("profile")}
>
<NavigationBar title="Profile" component={Profile} passProps={{ showFilter: this.state.showFilter }} />
</TabBarIOS.Item>
</TabBarIOS>
Within my NavigationBar.js, I simply render out a NavigatorIOS
<NavigatorIOS
ref="nav"
initialRoute={{ ...this.props }}
style={{
flex: 1
}}
/>
When a User clicks the Filter button, this.state.showFilter is updated in TabBar. It's then passed down into NavigatonBar correctly, the render() function inside of NavigationBar is executed however,
At this point, my component won't re-render the component listed in initialRoute (Profile)
Is there any way to achieve this? When clicking the Filter I need to set an optional variable inside of Profile to hide and show the Filter Modal
I'm not sure whether there is a more elegant solution to this, but I have one that works.
I added the following code inside of NavigationBar:
shouldComponentUpdate(nextProps, nextState) {
this.refs.nav.replace({...nextProps})
return true;
}
This will force a reload of the initialRoute component.

NavigatorIOS React native translucent

I'm using a NavigatorIOS for the routing of my app. I would like to display just the back button without any title or bar even translucent.
Is it possible ?
Or I must use another module ?
Currently, I have this :
<NavigatorIOS
ref='nav'
tintColor="white"
style={{flex: 1}}
initialRoute={{
title: 'Splash',
navigationBarHidden: true,
component: SplashScene
}}/>
Thanks a lot for your help,
Margot
You can pass translucent prop to NavigatorIOS's route, like so:
<NavigatorIOS
ref='nav'
tintColor="white"
style={{flex: 1}}
initialRoute={{
title: 'Splash',
navigationBarHidden: true,
translucent: true,
component: SplashScene
}}
/>

TabBarIOS on React Native not working as expected

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.
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
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...
When I tried this, the "TabBarItemIOS" seems to crash with an error 'Invariant Violation: onlyChild must be passed a children with exactly one child.' when the nested component a "NavigatorIOS" like in your example. It seems to work when child is a "View" component though. Did you get your code working?
Not sure exactly what you are trying to do. To get the tabBarIOS to work you need to as you say start with
var {
AppRegistry,
StyleSheet,
Text,
View,
NavigatorIOS,
TabBarIOS,
} = React;
Then create your class. Then create your constructor which starts which tab you want to be selected then you need to make methods that change the selected tab - when a tab is selected it is blue. then your render return with each TabBarIOS, inside each TabBarIOS.item, you must call the page you want it to go to
class example extends React.Component{
constructor(props){
super(props)
this.state = {
selectedTab: 'sassi',
}
}
homeHandleChange(){
this.setState({
selectedTab: 'home',
})
};
aboutHandleChange(){
this.setState({
selectedTab: 'about',
})
};
creditHandleChange(){
this.setState({
selectedTab: 'credits',
})
};
render() {
return (
<View style={styles.container}>
<View style={styles.footer}>
<TabBarIOS>
<TabBarIOS.Item
title="home List"
selected={this.state.selectedTab === "home"}
icon={require("./App/assets/youricon.png")}
onPress={this.homeHandleChange.bind(this)} >
<View style={styles.main}>
<home></home>
</View>
</TabBarIOS.Item>
<TabBarIOS.Item
title="credits"
selected={this.state.selectedTab === "credits"}
icon={require("./App/assets/yourIcon.png")}
onPress={this.creditsHandleChange.bind(this)} >
<View style={styles.main}>
<credits></credits>
</View>
</TabBarIOS.Item>
<TabBarIOS.Item
title="About"
selected={this.state.selectedTab === "about"}
icon={require("./App/assets/aboutIcon.png")}
onPress={this.aboutHandleChange.bind(this)} >
<View style={styles.main}>
<About></About>
</View>
</TabBarIOS.Item>
</TabBarIOS>
</View>
</View>
);
}
};
I got the same issue. But yes there are example from the UIExplorer that show the basic usage of icon. But unfortunately there's only 12 default system icons available now. Source code here: https://github.com/facebook/react-native/blob/master/React/Views/RCTTabBarItem.m#L28
I'm not quite familiar with object-c code so I'm still confused on how to set custom icon. But you can leave it like this for now(hope someone could get a better solution soon):
<TabBarIOS
selectedTab={this.state.selectedTab}>
<TabBarItemIOS
accessibilityLabel="Schedule"
title="Schedule"
name="scheduleTab"
icon={{}}
selected={this.state.selectedTab === 'scheduleTab'}
onPress={() => {
this.setState({
selectedTab: 'scheduleTab'
});
}}>
<NavigatorIOS
style={styles.container}
initialRoute={{
title: 'Schedule',
component: SchedulePage,
}}
/>
</TabBarItemIOS>
</TabBarIOS>