Set width and height to React-native modal - react-native

I can't configure modal height and width using style property. Is there any other way to set modal height and width?
<Modal style={{height: 300, width: 300}}
visible={this.state.isVisible}
onRequestClose={this.closeModal}>
</Modal>
The code above doesn't work.

According to the Modal documentation, there is no style prop to be set.
You can try setting the <View> dimensions inside the <Modal> instead:
<Modal transparent={true}
visible={this.state.isVisible}
onRequestClose={this.closeModal}>
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'}}>
<View style={{
width: 300,
height: 300}}>
...
</View>
</View>
</Modal>

Try adding margin:0 in the style of the <Modal> Component

In the Modal documentation it is not mentioned it but it has a style property.
The following works for me.
<Modal
style={styles.modalContent}
isVisible={this.state.isVisible}
onBackdropPress={this.closeModal}
>
<Component {...componentProps} />
</Modal>
const styles = StyleSheet.create({
modalContent: {
justifyContent: 'center',
alignItems: 'center',
margin: 0
},
});

Only thing that solved it was passing transparent={true} in the Modal props

The following worked for me after i have tried other methods, I had to give the modal some style.
<Modal isVisible={this.state.isModalAddCompanionVisible} >
<Button
block
style={{ marginTop: 20}}>
<Text style={{ color: "white" }} >Add Another Companion</Text>
</Button>
<View style={{backgroundColor: "white", height: 120}}>
</View>
<View style={{flexDirection: 'row'}}>
<Button
block
style={{ marginTop: 0,width: '40%'}} onPress={this._toggleModalAddCompanion.bind(this)} >
<Text style={{ color: "white" }} >Cancel</Text>
</Button>
<Button
block
style={{ marginTop: 0,width: '60%',}} >
<Text style={{ color: "white" }} >Add</Text>
</Button>
</View>
</Modal>

Related

Unable to click on icons in React Native Modal even with zIndex

I have a problem where the two materialicons / touchables in the view can't be clicked. (refer to the code section with the View and zIndex.
I may just be nesting the views wrongly.
When i add a zIndex, it fixes for IOS, but dosen't work for Android.
When i remove the ZIndex, it fixes for Android but dosen't work for IOS.
When i remove the whole it works on both, but i lose the
ability to do the space-between as i want to put the icons on the
opposite end of the row.
Would love some advice on why this is the case. Thank you.
<Modal
visible={props.showFilter}
backdropOpacity={0.3}
transparent={true}
animationType={'slide'}
>
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center'}}>
<View style={{
backgroundColor:'white',
width: wp('100%'),
height: hp('80%')}}>
>>> <View style={{zIndex:10, flexDirection: 'row', justifyContent: 'space-between'}}>
<TouchableOpacity
onPress={()=> props.close()}
style={{ zIndex:10, paddingHorizontal: 10,top: hp('5%') }} >
<MaterialIcon
name="close"
size={30}
color="#828282"
/>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> props.showHelp(true)}
style={{ zIndex:10, paddingHorizontal: 10,top: hp('5%') }} >
<MaterialIcon
name="help"
size={30}
color="#828282"
/>
</TouchableOpacity>
</View>
<View style={{flex:1, alignItems: 'center', paddingTop: 40,}}>
<Text style ={{ fontSize:wp('3.5%'),
fontFamily: 'Gilroy-Bold',
color: '#4F4F4F',
marginBottom: hp("2%"),}}>Filter</Text>
</View>
</View>
</View>
</Modal>
I took a crash course on flex and solved it using good flex skills then relying on Z-Index
https://medium.com/#ManningBooks/in-depth-styling-with-react-native-and-flexbox-ee1bda2c5b24
<View style={{flex:1}}>
<View style ={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
<TouchableOpacity
onPress={()=> props.show(false)}
style={{ flex:1, paddingHorizontal: 10,top: hp('5%') }} >
<MaterialIcon
name="close"
size={30}
color="#828282"
/>
</TouchableOpacity>
<TouchableOpacity
onPress={()=> console.log("onpress")}
style={{ flex:0, paddingHorizontal: 10,top: hp('5%') }} >
<Text
style ={{
color: "#828282",
fontFamily: 'Gilroy-Regular',
textDecorationLine: 'underline',
fontSize:wp('3.5%')}}> Refresh </Text>
</TouchableOpacity>
</View>
<View style ={{flex:8, alignItems: 'center'}}>
<Text style ={{ fontSize:wp('3.5%'),
fontFamily: 'Gilroy-Bold',
color: '#4F4F4F',
marginBottom: hp("2%"),}}>Filter Tags</Text>
<Text style={{
marginBottom: hp("2%"),
color: "#828282",
fontFamily: 'Gilroy-Regular',
fontSize:wp('3.5%'),
width: '80%'
}}>
text
</Text>
</View>
</View>

Adapt Webview height depending on the content inside

I am using Webview component in React Native to render HTML content. The problem I am facing is that I cannot make the height variable so it can adapt to any content it has.
return (
<View style={{flex: 1}}>
<Header
containerStyle={{
backgroundColor: 'white',
borderBottomColor: '#dee0e2',
borderBottomWidth: 1
}}
centerComponent={{ text: 'Noticia', style: { color: 'black' } }}
leftComponent={
<TouchableHighlight onPress={() => navigate('homeScreen')}>
<Text style={{color: '#4289f4'}}>AtrĂ¡s</Text>
</TouchableHighlight>
}
/>
<ScrollView style={{padding: 5}}>
<View style={{flexDirection: 'column'}}>
<Text style={{textAlign: 'center', fontSize: 17, fontWeight: 'bold'}}>{noticia[0].titulo}</Text>
<Image style={{width: '100%', height: 200, alignSelf: 'stretch', marginTop: 10}} source={{uri: noticia[0].imagen}} />
<View style={{height: 200, backgroundColor: 'red'}}>
<WebView
source={{html: noticia[0].articulo}}
scalesPageToFit={(Platform.OS === 'ios') ? false : true}
/>
</View>
</View>
</ScrollView>
</View>
);
If I don't give the <View>' that contents ' <Webview>' a height it does show me nothing. Tried already withflex: 1` but nothing changes.
In your parent view (root view directly under return()) style it as
height: '100%',
width: '100%'
It will adopt to any size screen.

Adding a images behind header react-native

I want to add multiple images behind header with a swiper. I tried implementing this using Native-base and react-native-swiper. However, what was resulted is, as following.
Following is my code
<View style={{flex:1, elevation:2}}>
<Swiper style={StyleSheet.flatten( {backgroundColor: '#fff'})}>
<ImageBackground
source={require('../../img/auth_background.png')}
style={this.styles.backgroundStyle}
imageStyle={this.styles.backgroundImage}
>
<Header style={{
marginTop: StatusBar.currentHeight,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
}}>
<StatusBar
backgroundColor={Colors.statusBar}
barStyle="light-content"
/>
<Left>
<Button transparent onPress={this.handleBackButtonClick} small={true}>
<Icon name='ios-arrow-back' size={30} color={Colors.textWhite}/>
</Button>
</Left>
<Body/>
<Right/>
</Header>
</ImageBackground>
<ImageBackground
source={require('../../img/auth_background.png')}
style={this.styles.backgroundStyle}
imageStyle={this.styles.backgroundImage}
>
<Header style={{
marginTop: StatusBar.currentHeight,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
borderBottomWidth: 0, shadowOffset: {height: 0, width: 0},
shadowOpacity: 0, elevation: 0
}}>
<StatusBar
backgroundColor={Colors.statusBar}
barStyle="light-content"
/>
<Left>
<Button transparent onPress={this.handleBackButtonClick} small={true}>
<Icon name='ios-arrow-back' size={30} color={Colors.textWhite}/>
</Button>
</Left>
<Body/>
<Right/>
</Header>
</ImageBackground>
</Swiper>
</View>
I want to fix the header so that it won't also swipe with the images. Can anyone help me with this? Thanks in-advance.
React-Native-Swiper in android will display blank content when swiper height and width aren't shown.
please add fixed height and width to view or to the swiper
Keep your Header outside Swiper and make its position absolute
<Header style={{
marginTop: StatusBar.currentHeight,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
position:'absolute',
top:0,
left:0
}}>
I found the solution using zIndex and position. I guess there is some issue when using native-base header and container with zIndex.
<View style={{flex: 1}}>
<View style={{flex:1, zIndex: 2, position: 'absolute', marginTop: StatusBar.currentHeight, marginLeft:'2%'}}>
<Button iconLeft transparent>
<Icon name='ios-arrow-back' size={30} color={Colors.textWhite}/>
</Button>
</View>
<View style={{width: '100%', height: '50%', zIndex: 1, position: 'absolute'}}>
<Swiper style={StyleSheet.flatten( {backgroundColor: '#fff',zIndex:1, flex:1})}>
<ImageBackground
source={require('../../img/auth_background.png')}
style={this.styles.backgroundStyle}
imageStyle={this.styles.backgroundImage}
>
</ImageBackground>
<ImageBackground
source={require('../../img/auth_background.png')}
style={this.styles.backgroundStyle}
imageStyle={this.styles.backgroundImage}
>
</ImageBackground>
</Swiper>
</View>

React native Text Input with Left and Right View mode

I am trying to build a Search component with Textfield with both right view and left view(Images at both end). Text Input wraps itself to the length of the content, so component no covering entire parent width. How to stretch the component so that entire component occupies the full width of parent.
<View
style={{
flexDirection: "row",
height: 40,
alignSelf: "stretch",
backgroundColor: "red"
}}
>
<Image
source={require("./image_assets_rn/search_rn.png")}
style={{ alignSelf: "center" }}
/>
<TextInput
placeholder="Search Jet"
style={{ backgroundColor: "white", alignSelf: "stretch" }}
/>
<Image
source={require("./image_assets_rn/search_rn.png")}
style={{ alignSelf: "center" }}
/>
</View>;
Below is the output. I want to stretch the textfield along the main axis(width)
It can be achieved giving the TextInput flex:1, there is no need to use stretch.
<View style={styles.container}>
<View style={{flexDirection:'row', width: 300, height:40,backgroundColor:'lightgray'}}>
<Image
source={require('./image_assets_rn/search_rn.png')}
style={{ height: 30, width: 20,margin: 5}}
/>
<TextInput
placeholder="Search Jet"
style={{backgroundColor:'white', flex:1}}
/>
<Image
source={require('./image_assets_rn/search_rn.png')}
style={{ height: 30, width: 20, margin:5}}
/>
</View>
</View>
Check it on snack
I recommend you this tutorial to have a grasp on how flexbox works.
Hope it helps!
I just add component flexbox in each component that wrapped in component View. I think it will make every component follow the parent width.
<View style={{ flexDirection:'row',
height:40,
alignSelf:'stretch',
backgroundColor:'red' }}>
<Image source={ require('./image_assets_rn/search_rn.png')}
style={{ flex:1, alignSelf:'center' }}/>
<TextInput placeholder="Search Jet"
style={{ flex:1, backgroundColor:'white',
alignSelf:'stretch' }}/>
<Image source={ require('./image_assets_rn/search_rn.png')}
style={{ flex:1, alignSelf:'center' }}/>
</View>
If you want to learn more about Flexbox maybe you can read on the Official Web ReactNative.
I hope this can help you :))

react-native bring sub view to front

I have added a dropdown and when I open it, the dropdown menu is hidden behind the views that have been added afterwards. How can I bring the dropdown view to front in react-native. I have now used an overlay and it brings the view to front but the view is not set in its normal location, instead it is displaying as the top most element.
In main render I have a listView:
<ListView ref='listView'
style = {{ backgroundColor: '#EAEAEC'}}
dataSource={this.state.dataSource}
automaticallyAdjustContentInsets={false}
renderRow={this.renderRow}/>
In render row of listView:
return (
<View style={{backgroundColor: '#FFF', marginBottom:5, shadowColor : '#BCBCBC', shadowOffset: {width: 0, height: 0}, shadowOpacity: 0.7, height:150}} >
<View style={{flex:1, flexDirection:'row',justifyContent: 'center', alignItems: 'center', backgroundColor: 'yellow', height:100}}>
<Overlay isVisible={true}><List /></Overlay>
</View>
<View style={{flex:5, flexDirection:'row',justifyContent: 'flex-end', alignItems: 'center', paddingTop:17, marginBottom:10}}>
<View style={{flex:1}}/>
<View style={{flex:1, alignItems: 'center'}}><Image style={styles.imageStyle} source={require('./audit_new_u.imageset/audit_new_u.png')} /></View>
<View style={{flex:1, alignItems: 'center'}}><Image style={styles.imageStyle} source={require('./audit_inProcess_u.imageset/audit_inProcess_u.png')} /></View>
<View style={{flex:1, alignItems: 'center'}}><Image style={styles.imageStyle} source={require('./audit_complete_s.imageset/audit_complete_s.png')} /></View>
<View style={{flex:1}}/>
</View>
</View>
);
Now the dropdown which is actually the List tag in render row which I have placed in overlay tags is now placed on top of the screen.
Add {zIndex: 999}
<View style={{zIndex: 999}}>
{...anything}
</View>
You have to make sure all the sub views inside your main view have a zIndex style property.
Like so:
const [myIndex, setMyIndex] = useState(1)
<View>
<View style={{zIndex: 3}}></View>
<View style={{zIndex: myIndex}}>
<Button onPress={() => setMyIndex(4)} title='Press to show above the other' />
</View>
<View style={{zIndex: 2}}></View>
</View>
Just add zIndex: -1 in the style of the view, you want to go behind in the hierarchy.
appActivityIndicatorOverlay: {
height: '100%',
width: '100%',
opacity: 0.8,
position: 'absolute',
elevation: (Platform.OS === 'android') ? 50 : 0,
backgroundColor: 'white',
color: theme.colors.brand.primary,
zIndex: 100
},