View有一个阴影设置,但在React Native中无法有效计算阴影。

huangapple go评论78阅读模式
英文:

View has a shadow set but cannot calculate shadow efficiently in React Native

问题

我在iOS上收到这个警告。

类型为RCTView的视图具有阴影设置,但无法高效计算阴影。考虑设置背景颜色以修复此问题,或将阴影应用于更具体的组件。

可以追踪到此问题的来源:https://github.com/facebook/react-native/commit/e4c53c28aea7e067e48f5c8c0100c7cafc031b06。

我不明白是什么导致了这个警告。这些是应用于引发问题的<View />的样式:

const styles = StyleSheet.create({
  borderColor: '#EDAE49',
  borderWidth: 2,
  borderRadius: 7,
  backgroundColor: '#EDAE49',
  padding: 7,
  marginBottom: 5,
  width: '100%',
  shadowColor: '#000',
  shadowOffset: {width: 0, height: 2},
  shadowOpacity: 0.5,
  shadowRadius: 2,
  elevation: 2,
});

在这里引起效率低下的原因是什么?这里设置了backgroundColor,并且backgroundColor没有透明度。

英文:

I am getting this warning on iOS.

> View ... of type RCTView has a shadow set but cannot calculate shadow efficiently. Consider setting a background color to fix this, or apply the shadow to a more specific component.

which can be tracked down to here https://github.com/facebook/react-native/commit/e4c53c28aea7e067e48f5c8c0100c7cafc031b06.

I do not understand what is causing the warning. This are the styles applied to the &lt;View /&gt; that is causing the problem.

const styles = StyleSheet.create({
  borderColor: &#39;#EDAE49&#39;,
  borderWidth: 2,
  borderRadius: 7,
  backgroundColor: &#39;#EDAE49&#39;,
  padding: 7,
  marginBottom: 5,
  width: &#39;100%&#39;,
  shadowColor: &#39;#000&#39;,
  shadowOffset: {width: 0, height: 2},
  shadowOpacity: 0.5,
  shadowRadius: 2,
  elevation: 2,
});

What is causing the inefficiency here? There is backgroundColor set, no transparency in the backgroundColor.

答案1

得分: 10

根据我的理解,如果你将阴影放在一个没有可见属性(例如背景颜色)的包装组件上,阴影会基于子组件来计算,从而降低效率。
解决方法是简单地将阴影移到一个新的样式属性并传递给子组件。

<View style={styles.containerWithShadow}>
    <Icon name="arrow-down-bold" />
</View>

如果View样式没有背景颜色,这将触发警告。阴影将隐式根据Icon的形状计算。

作为解决方法,你可以在View样式中添加一个实心背景,如果这不影响你的整体设计,或者你可以从View样式中移除阴影,并为Icon创建另一个带阴影的样式:

<View style={styles.container}>
    <Icon name="arrow-down-bold" style={styles.iconWithShadow} />
</View>
英文:

From my understanding, this happens if you put the shadow on a wrapper component that doesn't have a visible property (e.g. background color), the shadow instead gets calculated based on the child component, thus making it less efficient.
The solution is to simply move the shadow to a new style property and pass it to the children instead.

&lt;View style={styles.containerWithShadow}&gt;
    &lt;Icon name=&quot;arrow-down-bold&quot; /&gt;
&lt;/View&gt;

This will trigger the warning if the View style doesn't have a background color. The shadow will be implicitly calculated based on the shape of the Icon.

As a solution, you can either give the View style a solid background if that doesn't affect your overall design, or you can remove the shadow from the View style and create another style with shadow for the Icon:

&lt;View style={styles.container}&gt;
    &lt;Icon name=&quot;arrow-down-bold&quot; style={styles.iconWithShadow} /&gt;
&lt;/View&gt;

答案2

得分: 2

我在iOS中遇到了一个警告消息的问题。

警告(建议):类型为 RCTView 的第 3535 号视图设置了阴影,但无法高效计算阴影。考虑设置背景颜色来修复此问题,或将阴影应用于更具体的组件。

检查了警告消息后,我发现我已经将阴影应用到了某些视图上,但忘记了应用backgroundColor,所以才会出现这个警告消息。

在下面的代码中,出现了警告消息:

const styles = {
  menuStyle: {
    menuProviderWrapper: {
      elevation: 0,
      shadowColor: '#000',
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
      borderRadius: 10,
    },
    backdrop: {
      shadowColor: '#000',
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
    },
  },
};

在添加了背景颜色后,警告消息消失了:

const styles = {
  menuStyle: {
    menuProviderWrapper: {
      elevation: 0,
      shadowColor: '#000',
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
      borderRadius: 10,
      backgroundColor: theme.whiteColor,
    },
    backdrop: {
      shadowColor: '#000',
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
      backgroundColor: theme.whiteColor,
    },
  },
};
英文:

I have this problem in iOS a warning message.

> WARN (ADVICE) View #3535 of type RCTView has a shadow set but cannot
> calculate shadow efficiently. Consider setting a background color to
> fix this, or apply the shadow to a more specific component.

After check the warning message I come know that I have applied shadow to some Views but I forgot to apply backgroundColor so it was creating this warning message.

here is my code in which it was giving warning

const styles = {
  menuStyle: {
    menuProviderWrapper: {
      elevation: 0,
      shadowColor: &#39;#000&#39;,
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,

      elevation: 1,
      borderRadius: 10,
    },
    backdrop: {
      shadowColor: &#39;#000&#39;,
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
      
    },
  },
};

After adding backgroundColor the warning got disappear

const styles = {
  menuStyle: {
    menuProviderWrapper: {
      elevation: 0,
      shadowColor: &#39;#000&#39;,
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,

      elevation: 1,
      borderRadius: 10,
      backgroundColor: theme.whiteColor,
    },
    backdrop: {
      shadowColor: &#39;#000&#39;,
      shadowOffset: {
        width: 0,
        height: 1,
      },
      shadowOpacity: 0.18,
      shadowRadius: 1.0,
      elevation: 1,
      backgroundColor: theme.whiteColor,
    },
  },
};

答案3

得分: 1

首先,你需要找出是哪个组件引起了这个问题。找到之后,你应该:

  • 将背景更改为非透明背景
  • 或者去除阴影设置shadowOpacity:0

要找到一个组件:

  1. 在React/Views/RCTView.m中
RCTLogAdvice(
          @"View #%@ of type %@ has a shadow set but cannot calculate "
           "shadow efficiently. Consider setting a background color to "
           "fix this, or apply the shadow to a more specific component.",
          view.hash,
          [view class]);

获取该哈希值,并在Flipper中查找此哈希值对应的组件。

在我的情况下,我正在使用react-navigation,它向我正在使用的组件添加了带有透明背景的阴影。

英文:

First, you need to find out which component creates that problem. After you find it you should:

  • change the background to the not transparent one
  • OR remove shadow or set shadowOpacity:0.

To find a component:

  1. in React/Views/RCTView.m
RCTLogAdvice(
          @&quot;View #%@ of type %@ has a shadow set but cannot calculate &quot;
           &quot;shadow efficiently. Consider setting a background color to &quot;
           &quot;fix this, or apply the shadow to a more specific component.&quot;,
          view.hash,
          [view class]);

get that hash and in flipper View有一个阴影设置,但在React Native中无法有效计算阴影。

you can find the component using this hash.

In my case i was using react-navigation that adds shadow to a component that i am using with transparent background.

huangapple
  • 本文由 发表于 2023年2月6日 03:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定