【问题标题】:How to align Icon vertically in IOS devices?如何在IOS设备中垂直对齐Icon?
【发布时间】:2018-11-23 17:28:53
【问题描述】:

我想在 react-nativeIOS 应用程序中的 TouchableHighlight 中对齐这个选项图标,但是当我实现它时没有使用 textAlignVerticle属性,因为它是 android 唯一的属性。我如何才能获得在 android 和 IOS 中都适用的替代方案?

<TouchableHighlight  
                style={[{position: 'absolute', bottom: 20, zIndex:999999, right: 155, borderRadius: 25}, (this.state.searchList)? { backgroundColor: 'red' }: {backgroundColor: 'black'}]}
                onPress={()=>this.props.navigation.navigate('SearchSetting')}
            >
  <Icon name="options" style={{height: 50, width: 50, color: 'white', textAlign: 'center', textAlignVertical: 'center'}} />
</TouchableHighlight>

【问题讨论】:

标签: ios react-native


【解决方案1】:

React Native 的一个组件使用了 flexbox 算法(与 CSS Flexbox 非常相似)。因此,您可以使用父组件中的justifyContentalignItems 将任何子组件垂直或水平对齐。 justifyContentalignItems 都依赖于 flexDirection。默认 flexDirection 在 react native 中是 column

flexDirection: 'row';

您可以将子组件垂直对齐justifyContent,水平对齐父组件中的alignItems

试试下面的代码:

<TouchableHighlight  
  style={[
    { 
      position: 'absolute', 
      bottom: 20, 
      zIndex:999999, 
      right: 155, 
      borderRadius: 25,
      backgroundColor: '#0000ff'
     }
  ]}>

  <View 
    style={{
      flex: 1,
      width: 100,
      height: 100,
      justifyContent: 'center',
      alignItems: 'center'
    }}>
    <Ionicons name="ios-home" size={20} color={'#FFF'} />
  </View>
</TouchableHighlight>

我不知道您使用的是什么图标组件。我正在使用react-native-vector-icons。所以我不需要在 icon 组件的 style props 中设置 widthheight 来获取 icon 大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2012-09-13
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多