我有一个ScrollView,顶部有一个背景 colored颜色 ,底部有另一个不同的 colored颜色 .

当用户滚动浏览内容时,视图会反弹(弹性过度延伸),我如何根据滚动方向使背景与顶部或底部一致?

推荐答案

在iOS上,您可以在ScrollView的顶部渲染间隔View,并使用contentInset将其渲染为"屏幕外",contentOffset将初始滚动位置设置为偏移插图:

render() {
  const isIos = Platform.OS === 'ios'
  const SPACER_SIZE = 1000; //arbitrary size
  const TOP_COLOR = 'white';
  const BOTTOM_COLOR = 'papayawhip';
  return (
    <ScrollView
      style={{backgroundColor: isIos ? BOTTOM_COLOR : TOP_COLOR }}
      contentContainerStyle={{backgroundColor: TOP_COLOR}}
      contentInset={{top: -SPACER_SIZE}}
      contentOffset={{y: SPACER_SIZE}}>

      {isIos && <View style={{height: SPACER_SIZE}} />}
      //...your content here

    </ScrollView>
  );
}

因为contentInsetcontentOffsetiOS only,所以这个例子的条件是在Android上优雅地降级.

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

使用NextJS映射从数组中提取用户角色

Redux将旧状态迁移到新状态

Error: Invariant Violation: Touchable child must either be native or forward setNativeProps to a native component stack

应用程序主体尚未注册

在向后滑动时执行操作(react-navigation )

无法在 React Native iOS 模拟器中按 URI 显示图像

我可以在状态内有一个函数来响应吗?

给定 APK 文件,我如何检测应用程序是否使用 React Native?

React-native 在浏览器中打开链接并返回应用程序

带有 react-navigation 的 DrawerNavigation 标头

如何使 React Native Animated.View 可点击?

React Native 创建自定义组件/库

require() 必须有一个字符串文字参数 React Native

React Native Android 文本组件额外填充

如何修复 npm start cannot determine native SDK version错误?

React Native Image 不显示,我该如何解决?

如何在 React Native 中优化图像

React Native 元素,未生成 index.ios.js 或 index.android.js

com.facebook.react.bridge.readablenativemap 不能转换为 java.lang.string