且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在React Native中使用框阴影创建UI

更新时间:2023-12-03 18:03:34

您将不得不为iOS和Android使用不同的样式道具.

You will have to use different style props for iOS and Android.

Android

这对于android非常简单,只需使用 elevation 样式道具(请参见

It's very simple for android, just use the elevation style prop (See docs) . An example:

boxWithShadow: {
    elevation: 5
}

iOS

在iOS中,您具有更大的灵活性,请使用Shadow道具(请参见 docs ).例子:

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,  
}

摘要

总而言之,要获得两个平台的盒子阴影,请同时使用两组样式道具:

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
}

注意: 请勿在所有 iOS 中使用 overflow:'hidden'; 阴影消失了.

Attention: Do not use overflow: 'hidden';, in iOS all of the shadows disappear by this property.