Javascript React Native Listview 留下空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29496054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:27:54  来源:igfitidea点击:

React Native Listview leaving space

javascriptioslistviewreact-native

提问by ilansas

I'm trying to implement a listview in React Native. Everything was fine when I was just calling the component Accountsbut since I put it into a NavigatorIOS the Listview is leaving some space before the first item : see hereand when I scroll here.

我正在尝试在 React Native 中实现一个列表视图。当我只是调用组件时,一切都很好,Accounts但是由于我将它放入 NavigatorIOS,Listview 在第一项之前留下了一些空间:请参阅此处以及当我滚动此处时

Here is my code :

这是我的代码:

index.ios.js

index.ios.js

var RemoteX1 = React.createClass({

  render: function() {
    return (
      <NavigatorIOS
        style={styles.container}
        initialRoute={{
          title: 'Accounts',
          component: Accounts,
        }}/>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

accounts.js

账户.js

var people = [
  {
    title: "Papa",
    favouriteChannels: []
  },
  {
    title: "Maman",
    favouriteChannels: []
  },
  {
    title: "Kids",
    favouriteChannels: []
  },
  {
    title: "Invité",
    favouriteChannels: []
  }
];

var Accounts = React.createClass({
  getInitialState: function() {
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    return {
      dataSource: ds.cloneWithRows(people),
    }
  },
  renderRow: function(person) {
    return (
      <TouchableHighlight onPress={this.onPressRow}>
        <Text style={styles.account}>{person.title}</Text>
      </TouchableHighlight>
    );
  },
  render: function() {
    return (
      <View style={styles.container}>
        <View style={styles.panel}>
          <Text style={styles.icon}>&#xF0C6;</Text>
          <Text style={styles.welcome}>BIENVENUE</Text>
          <ListView 
            dataSource={this.state.dataSource}
            renderRow={this.renderRow}
            style={styles.listView} />
        </View>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  },
  panel: {
    backgroundColor: '#67F072',
    alignItems: 'center',
    justifyContent: 'center',
    paddingLeft: 50,
    paddingRight: 50,
  },
  icon: {
    color: '#67B7F0',
    fontFamily: 'CanalDemiRomainG7',
    fontSize: 40
  },
  welcome: {
    color: '#67B7F0',
    fontFamily: 'CanalDemiRomainG7'
  },
  account: {
    color: '#000000',
    height: 30,
    alignSelf: 'center',
    fontFamily: 'CanalDemiRomainG7'
  },
})

Does anyone has any idea of what's going on ? Thanks

有没有人知道发生了什么?谢谢

回答by ilansas

Ok so, I found the answer, I post it here for anyone who run into the same issue.

好的,我找到了答案,我将其发布在这里供遇到相同问题的任何人使用。

An issue has been posted on Github here. It is necessary to add to the ListViewthe parameter automaticallyAdjustContentInsets={false}. Problem Solved.

此问题已得到张贴在Github这里。有必要添加到ListView参数中automaticallyAdjustContentInsets={false}。问题解决了。

回答by drikoda

This only happens when you have a ScrollView or ListView with TabBarIOS. You can remove the extra space at the top if you put automaticallyAdjustContentInsets={false}

这仅在您拥有带有 TabBarIOS 的 ScrollView 或 ListView 时发生。如果你把,你可以删除顶部的额外空间automaticallyAdjustContentInsets={false}

However, the ScrollView will not completely be shown because the bottom portion of it will be hidden under the TabBarIOS. To fix this add contentInset={{bottom:49}}adjust the height according to your needs.

但是,ScrollView 不会完全显示,因为它的底部将隐藏在 TabBarIOS 下。要解决此问题contentInset={{bottom:49}},请根据您的需要调整高度。

here is the code:

这是代码:

<ListView
  contentInset={{bottom:49}}
  automaticallyAdjustContentInsets={false}
>

回答by TomTom

If you're trying to achieve this in android..contentInset is iOS specific, to manage this on android you need to set the property inside of <ListView contentContainerStyle={styles.contentContainer}> </ListView>

如果你想在 android..contentInset 中实现这一点是 iOS 特定的,要在 android 上管理它,你需要在里面设置属性 <ListView contentContainerStyle={styles.contentContainer}> </ListView>

Then in your stylesheets.create

然后在你的 stylesheets.create

var styles = StyleSheet.create({
  contentContainer: {
    paddingBottom: 100 
  }

* I realise that op is asking for an iOS solution, just adding the android solution for people passing and if op decides he's fed up with iOS

* 我意识到 op 需要一个 iOS 解决方案,只是为路过的人添加 android 解决方案,如果 op 决定他厌倦了 iOS