Elevation in React Native
Asked Answered
I

6

11

Here is my style defined in the React-Native screen. I have used the elevation property to achieve a box-shadow. But it's not working properly.

const styles = StyleSheet.create({
scrollContainer: {
    flex: 1,
},
container: {
    flex: 1,
    flexDirection: "row",
    flexWrap: "wrap",
    padding: 2
},
box: {
    margin: 8,
    width: Dimensions.get('window').width / 2 - 18,
    height: Dimensions.get('window').width / 2 - 18,
    justifyContent: "center",
    alignItems: "center",
    borderStyle: 'dashed',
    borderLeftWidth: 1,
    borderTopWidth: 1,
    borderRightWidth: 1,
    borderBottomWidth: 1,
    borderTopColor: 'black',
    borderBottomEndRadius : 8,
    borderTopStartRadius: 8,
    borderTopEndRadius: 8,
    borderBottomStartRadius: 8,
    borderBottomLeftRadius:8,
    borderBottomRightRadius:8,
    elevation: 5
},
navBar:{
    backgroundColor: "#000",
}
});

I have also tried using box-shadow but the same problem arises.

Imagery answered 6/12, 2018 at 8:26 Comment(1)
Have you tried specifing shadowColor: Colors.BLACK, shadowOffset: { width: 0, height: 8 }, shadowOpacity: 0.16, shadowRadius: 16? Values are clearly random :)Havard
S
40

Try to add following properties to the styles.box. You could change the values for better results.

// ...
box: {
  // ...
  shadowColor: '#000',
  shadowOffset: { width: 0, height: 2 },
  shadowOpacity: 0.5,
  shadowRadius: 2,
  elevation: 2,
},
// ...
Sarsenet answered 6/12, 2018 at 8:53 Comment(0)
T
3

you can use below code

          height: 150,
          width: '100%',
          backgroundColor: 'white',
          elevation: 5,
          shadowColor: '#000',
          shadowOffset: {width: 0, height: 0},
          shadowOpacity: 0.1,
          shadowRadius: 5,
Tetrahedron answered 15/5, 2020 at 5:18 Comment(0)
I
2

After little investigation, I found the working solution and here it is. There is a broad discussion over this problem on the github issue.

box: {
    margin: 8,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.5,
    shadowRadius: 2,
    elevation: 2,
}

rather only using elevation property. Also, make sure you give the proper margin to the box for proper spacing. I was lacking the margin in my case. Hope it's useful findings to help.

Imagery answered 6/12, 2018 at 10:24 Comment(0)
B
1

You can add this style in your View

 headerstyle:{
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.5,
    shadowRadius: 2,
    elevation: 2,
    alignSelf: 'stretch'
},
Beitnes answered 7/7, 2021 at 11:3 Comment(1)
shadow properties has not effect except elevation and shadowColorBackstop
B
1

I created this when I was having issues. Since then I have been using it in my every project. It works well in all of my projects unless the client wants something different.

Just copy and paste this and change the values in elevation & height or weight of shadowOffset as per your requirement.

 shadowOffset: {
  width: 0,
  height: 2,
},
shadowOpacity: 0.21,
shadowRadius: 6.65,
elevation: 6,

},

Belong answered 27/2 at 6:31 Comment(0)
T
0
chip: {
    padding: 10,
    height: 40,
    shadowColor: 'black',
    elevation: 3,
    borderRadius: 5,
    backgroundColor: '#fff'
  }
Tasso answered 21/1, 2023 at 5:49 Comment(1)
Please add explanations.Snaggletooth

© 2022 - 2024 — McMap. All rights reserved.