I am trying to create a UI in react native, the UI contains a box with outer shadow. using the image I have done that, but is there any proper way to do that?
For adding box shadows in Android, we can use the elevation prop, which uses the Android Elevation API. Next, import the StyleSheet again to style the card: // remember to import StyleSheet from react-native const styles = StyleSheet.
To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden' . to set overflow to 'hidden' on the outer View . And then we add the shadow styles in the inner view to add the shadow. elevation is needed for Android to show the shadow.
You will have to use different style props for iOS and Android.
Android
It's very simple for android, just use the elevation
style prop (See docs) . An example:
boxWithShadow: { elevation: 5 }
iOS
In iOS you have more flexibility, use the Shadow props (See docs). An example:
boxWithShadow: { shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.8, shadowRadius: 1, }
Summary
In summary, to get box shadow for both platforms, use both sets of style props:
boxWithShadow: { shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.8, shadowRadius: 2, elevation: 5 }
Attention: Do not use overflow: 'hidden';
, in iOS
all of the shadows disappear by this property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With