【问题标题】:How can I set the margin for View only on iOS devices?如何在 iOS 设备上设置仅查看的边距?
【发布时间】:2021-11-19 08:51:37
【问题描述】:

我遇到的问题如下:
我有一个视图,我只想为每个 iOS 设置 marginTop=20,我执行以下操作:

<View style={[styles.container,Platform === "ios" ? {marginTop:20}:{marginTop:0}]}>
</View>

但该代码不起作用。有人可以向我解释原因并给我一个解决方案吗?

【问题讨论】:

  • 您应该检查 Platform.OS 而不仅仅是 Platform
  • @The1993 是对的。您需要使用 Platform.OS

标签: android ios react-native react-hooks cross-platform


【解决方案1】:

你需要写 Platform.OS === "ios"。

【讨论】:

    【解决方案2】:

    这样改写样式:

    
    <View 
        style={[
          styles.container,
          { 
           marginTop: Platform.OS === "ios" ? 20 : 0 
          }
        ]}>
    </View>
    
    

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 2014-12-11
      • 2017-06-03
      • 2015-12-06
      • 2014-06-01
      • 1970-01-01
      • 2011-10-25
      • 2018-05-09
      • 1970-01-01
      相关资源
      最近更新 更多