Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add shadow around the image in react-native

Tags:

react-native

I need to add a shadow around the Image my image is a rectangular field and i need to add a shadow around that rectangular field

I want to do something like this: stackoverflow question

I wanted to know how to do this in react native that can be applicable for both android and ios

like image 431
nadeshoki Avatar asked Oct 27 '17 08:10

nadeshoki


People also ask

Is there box shadow in React Native?

For styling the Box Shadow in React Native, only the elevation feature is supported in the Android platform, while in the iOS platform, we can style the box-shadow in many ways by setting the opacity, color, height, width, radius, etc.

How do I apply shadow on view in React Native?

🤖 How to apply shadows on Android platform On Android, we need to use the elevation view style prop from react-native to add shadows. elevation: Sets the elevation of a view, using Android's underlying elevation API. This adds a drop shadow to the item and affects z-order for overlapping views.


2 Answers

Shadow is only for iOS. For Android you need Elevation. You could do something like this. I use it currently and works fine:

elevationLow: {
    ...Platform.select({
    ios: {
        shadowColor: '#000',
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.8,
        shadowRadius: 2,    
    },
    android: {
        elevation: 5,
    },
    }),
},
like image 76
sebastianf182 Avatar answered Sep 24 '22 23:09

sebastianf182


Wrap your Image inside View (for semantic clarity) and then define following style rules to the View:

shadow: {
  shadowColor: '#202020',
  shadowOffset: {width: 0, height: 0},
  shadowRadius: 5,
},

I made an example here: https://snack.expo.io/rJesdOgRZ. But atm "snack" is so freaking slow that it's difficult to check actual results. But at least the code is visible and works as a benchmark.

like image 29
Samuli Hakoniemi Avatar answered Sep 24 '22 23:09

Samuli Hakoniemi