Why isn't zIndex working in React Native? - ios

I've been trying to use the property zIndex on a ScrollView but it doesn't seem to be working. When I apply zIndex it looks like this:
As you can see, the ScrollView is being blocked by the input. I want the effect of having the ScrollView completely cover the other input when the suggestions come up, with zIndex. Here's what I have right now:
<View>
<Input
value={this.state.inputValue}
style={styles._input}
inputStyle={styles._text}
onChangeText={this.handleChangeText}
{...this.props}
/>
<View style={styles._spacing}></View>
{predictions.length ?
<ScrollView
style={styles._scroll}
>
{predictions.map(this.renderPrediction)}
</ScrollView>
: null}
</View>
Note that Input is a separate component that renders a specific TextInput. Also, this.renderPrediction returns this:
<Text
style={styles._text}
key={index}
onPress={this.handlePredictionPress.bind(null, prediction.description)}
>
{prediction.description}
</Text>
And finally here are my styles:
const styles = EStyleSheet.create({
input: StyleSheet.flatten([inputStyle]),
text: {
fontSize: 15,
},
spacing: {
height: 10
},
scroll: {
position: "absolute",
zIndex: 10,
width: "80%",
top: 50,
backgroundColor: "white"
}
});
Why isn't my zIndex attempt working and how can I get it to work as desired?

According to your image it looks like the zIndex is working as expected, the ScrollView is over the input. But the white background didn't catch.
Use contentContainerStyle for the background color behind the items.
<ScrollView style={styles._scroll} contentContainerStyle={styles._contentContainer} >
...
contentContainer: {backgroundColor: "white"}

Related

While using a custom header with react navigation for a react native app, I can't identify where this background is coming from?

I am using the default stack from react navigation, and in my navigator screen options I have my own custom header header: (props) => <Header props={props} />. In my screen, I have a background colour, and in my SafeAreaView I have a background colour. For illustration I have set them both to the same.
In my custom header I am applying margin of 15 horizontally as well as to the top. So it is the colour underneath the custom header.
My question is this: Where is this header background colour being set? (the light grey one)
Here you can see what I mean
I have tried headerStyle {backgroundColor}, but no difference (i believe because using header overrides a lot of these options? Unsure though as it is not stated in the documentation.)
For reference, I am basing it off of this documentation: https://reactnavigation.org/docs/native-stack-navigator/#options
However it appears that this background colour is being set somewhere else entirely? And so I'm not sure where.
I realize setting headerTransparent and applying margin for each screen is a viable solution but I figure there is a better way to simply set the background colour of this space correctly.
edit with header component:
export default function Header({ props }) {
const { navigation, route, options, back } = props;
const title = getHeaderTitle(options, route.name);
return (
<View style={styles.header}>
<View style={styles.container}>
{back && (
<IconButton
type="ion"
name="chevron-back"
size={26}
iconStyle={styles.backButton}
onPress={navigation.goBack}
/>
)}
<Text style={styles.title}>{title}</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
width: screenWidth - 30,
height: 60,
borderRadius: 10,
padding: 15,
marginTop: 15,
backgroundColor: colors.white,
alignSelf: "center",
flexDirection: "row",
alignItems: "center",
},
backButton: {
zIndex: 1,
},
title: {
flex: 1,
textAlign: "center",
flexDirection: "column",
fontSize: 24,
fontWeight: "bold",
// marginLeft: -26,
},
header: {
backgroundColor: colors.neutral95,
},
});

React native, shoutem/ui : navigating back on NavigationBar does not work on iOS

I am struggling to make back navigation arrow on shoutem/ui NavigationBar work on iOS. The navigation bar looks like this and works on Android as expected (tap on the arrow navigates to specific predefined view) :
The related layout is as follows :
import {
Text,
Button,
View,
Image,
Divider,
Spinner,
NavigationBar,
Caption
} from '#shoutem/ui';
render() {
const {navigate} = this.props.navigation;
if (this.state.submitted && this.props.loading) {
return (
<Spinner style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}} />
);
}
return (
<Container style={{ flex: 1, backgroundColor: '#ffffff' }}>
<Content>
<NavigationBar
styleName='no-border'
hasHistory
navigateBack={() => navigate('WelcomeScreen')}
/>
<Grid>
<Row style={{ height: 100, justifyContent: 'center', alignItems: 'center', paddingTop: 100 }}>
<Image
style={{ width: 96, height: 89 }}
source={require('../login-logo.png')}
blurRadius={1} />
</Row>
//some other rows and columns
</Grid>
</Content>
</Container>
);
}
}
On Android the following works as expected. On iOS (Xcode simulator) the navbar is displayed correctly, however tapping on it does nothing. No log events nor errors are generated as well. I assume that the navbar can somehow be overlaid by some other element. However, the same problem exists with the other views with different elements inside the grid below the navbar. Does anyone have experience with this issue? What is the most likely cause and what am I doing wrong?
The "no-border" styleName applied to NavigationBar turned out to be the offending code piece in this case. Replacement with "inline" worked around the issue while introducing a slightly distinguishable border below the NavigationBar. However, this looks like a bug and the following issue has been opened in shoutem/ui GitHub repository : https://github.com/shoutem/ui/issues/398

Why does adding a parent View Tag in React Native change the style?

Adding a View parent breaks my styles, why is this happening? Does React Native's View tag have some default styles that I'm unaware of?
Below is the Button component as well as a screenshot with and without the View Tag.
class Button extends Component {
render() {
return (
<View>
<TouchableOpacity style={styles.buttonStyle}>
<Text style={styles.textStyle}>Hello</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
textStyle: {
alignSelf: "center",
color: "#007aff",
fontSize: 16,
fontWeight: "600",
paddingTop: 10,
paddingBottom: 10
},
buttonStyle: {
flex: 1,
alignSelf: "stretch",
backgroundColor: "#fff",
borderRadius: 5,
borderWidth: 1,
borderColor: "#007aff",
marginLeft: 5,
marginRight: 5
}
});
The button is being rendered inside another component(A typical card component), if need I can provide that code as well.
Basically, your TouchableOpacity has all of your container styles, and this is going to cause the weird issue you are seeing. By defining flex:1, it means it will stretch itself to fill the area of the parent view. However, your parent view has not been flexed, so it will only fill the space that it needs to. It will however put your text component in however it seems fit, hence the weird result you are getting.
To debug this, I often will set backgroundColor: 'red', to see what flexbox is doing. In your case, i don't see the need for a parent view ? TouchableOpacity is simply an extension of view, so you should be able to do all your styling in it. If you are wanting to add a view though, i'd suggest wrapping it with your Touchable - otherwise you have the risk of messing up your onPresss. Something like
render() {
return (
<View style = {{height: 100, shadowColor: 'black', shadowRadius: 3}}> //RANDOM CARD CODE
<TouchableOpacity style={styles.buttonStyle}>
<View> //ANY STYLES HERE
<Text style={styles.textStyle}>Hello</Text>
</View>
</TouchableOpacity>
</View>
)};

TouchableOpacity onPress doesn't work inside ScrollView

(iOS Only)
<TouchableOpacity> doesn't respond if it is inside of a <ScrollView> :
It works properly in the simulator but not in a real device,
keyboardShouldPersistTaps="always" doesn't make any difference
Partial code:<ScrollView style={styles.scrollView}>
<TouchableOpacity style={styles.xButton} onPress={() => this._onClose()}>
any suggestions?
--- Code update -----
<ScrollView style={styles.scrollView}>
<TouchableOpacity style={styles.xButton} onPress={() => this._onClose()}>
<Image style = {styles.xImg} source = {require('../../images/xbtn.png')}/>
</TouchableOpacity>
{this._renderPricing()}
{this._renderServices()}
</ScrollView>
and the Styling looks like this:
scrollView:{
width: width,
height: height,
}, xButton: {
position: 'absolute',
zIndex: 1,
marginTop: '1%',
marginRight: '3%',
alignSelf: 'flex-end',
},xImg: {
resizeMode: 'contain',
aspectRatio: .6,
opacity: 0.5,
},
The issue was solved. It was caused because in my separate rendering methods the this._renderPricing etc I was changing the state too many times 👎 therefore the JS thread was occupied so the TouchableOpacity couldn't respond to touch events see RN documentation for a more detailed explanation if needed. Thank you very much for your answers.

Text input view not render properly

As a beginner on React Native, I decided to have a look at layouts. I have a component like this with a text input component and a text component.
class TextView extends Component{
render(){
return(
<View style = {{flex:1,justifyContent:'center',alignItems:'center',backgroundColor: 'pink'}}>
<TextInput style = {TextInputStyle.default}/>
<Text> Hello again! </Text>
</View>
)
}
}
After calling TextView component on root file index.ios.js the output seems like this.
As you can see only text if following the stylesheet but not the text input. Any help will be appreciated.
Edit
This is what TextInputStyle looks like
const TextInputStyle = StyleSheet.create({
default : {
color : 'darkgrey',
fontSize : 14,
backgroundColor : 'red',
height : 20,
width : 100,
}
})
First thing, you have not specified what's inside TextInputStyle.default, may be that is overriding the css style. Try to wrap TextInput in a view.
<View style = {{flex:1,justifyContent:'center',alignItems:'center',backgroundColor: 'pink'}}>
<View>
<TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1, width:150}}/>
</View>
<Text> Hello again! </Text>
</View>

Resources