react-navigation如何设置与动画值对应的tabBar边距

kir*_*imi 11 javascript react-native react-navigation

我正在使用react-navigation Tab Navigation.我的标签导航上方有一个标题,它可以折叠和展开对应的scrollView.

这是我的问题:当我一直向上滚动时,标题会崩溃,这就是我想要的,但tabBar将保持静态(请参阅照片).有没有办法我可以设置tabBar margin相应的滚动视图?这样marginTop当标题折叠时就没有了.

在此输入图像描述

const Header_Maximum_Height = 40;
const Header_Minimum_Height = 0;

export default class App extends Component {

  render(){
    return (
      <View style={{flex:1, marginTop:30}}>
        <AppContainer/>
      </View>
    )
  }
}

class HomeScreen extends Component{
      constructor()
    {
         super();
         this.AnimatedHeaderValue = new Animated.Value(0);
     }
render() {

const AnimateHeaderBackgroundColor = this.AnimatedHeaderValue.interpolate(
   {
    inputRange: [ 0, ( Header_Maximum_Height - Header_Minimum_Height )  ],
    outputRange: [ '#009688', '#00BCD4' ],
    extrapolate: 'clamp'
   });

const AnimateHeaderHeight = this.AnimatedHeaderValue.interpolate(
      {
       inputRange: [ 0, ( Header_Maximum_Height - Header_Minimum_Height ) ],
       outputRange: [ Header_Maximum_Height, Header_Minimum_Height ],
       extrapolate: 'clamp'
                  });
          return (
                  <SafeAreaView style={{flex:1}}>

                  <Animated.View style={{height:AnimateHeaderHeight,width:'100%', backgroundColor:'gray'}}>
                    <Text style={{color:'white'}}> Collapsible Expandable Header </Text>
                  </Animated.View>

                    <Animated.ScrollView
                                      scrollEventThrottle = { 16 }
                                      onScroll = { Animated.event(
                                        [{ nativeEvent: { contentOffset: { y: this.AnimatedHeaderValue }}}]
                                  )}>

                        <ImageBackground
                        style={{width:375, height:400}}
                        source={require('./assets/pizza.jpg')}>
                        </ImageBackground>

                 </Animated.ScrollView>
                 </SafeAreaView>
                          );
                        }
                      }
    const tabBarHeight = 100

    const AppTabNavigator = createMaterialTopTabNavigator({

      Home:{
      screen:HomeScreen,
      navigationOptions: {
          header: null,
          tabBarVisible:true,
          activeTintColor: '#e91e63',
        }
      }, {
        tabBarOptions: {
         showLabel: true,
          style: {
              backgroundColor: 'rgba(22, 22, 22, 0)',
              position: 'absolute',
              Top:  Dimensions.get('window').height-tabBarHeight,
              left:0,
              right:0,
//I initially set the margin to 45 but as I scroll up How can I set the marginTop to 0 when I reach the top screen.
              marginTop:45
          },
          labelStyle:{
            fontSize:15,
            color:"white"
          }
        }
       }
      )
Run Code Online (Sandbox Code Playgroud)

我也试过marginTop,this.AnimatedHeaderValue但似乎没有工作.任何建议或意见都会非常有帮助.

Bal*_*zar 3

listener您可以在Animated.event上添加一个,这将允许您获取 的值y

<Animated.ScrollView
  scrollEventThrottle={16}
  onScroll={Animated.event(
    [{ nativeEvent: { contentOffset: { y: this.AnimatedHeaderValue }}}],
    {
      useNativeDriver: true,
      listener: event => {
        const offsetY = event.nativeEvent.contentOffset.y
        this.onScrollChange(offsetY)
      }
    },
  )}
>
  <ImageBackground
    style={{ width: 375, height: 400 }}
    source={require('./assets/pizza.jpg')}
  />
</Animated.ScrollView>
Run Code Online (Sandbox Code Playgroud)

在这里,您只需要onScrollChange在组件中定义。因为您marginTop位于顶级<App />,所以更改会有点棘手,因此您可能会使用 Redux 或Context来更新和使用此值。

您还可以将边距置于同一水平并使用状态,但这将迫使您根据您的架构对每个页面执行相同的操作。


因为您需要在运行时更改tabBarOptions,所以您将需要创建并向tabBarComponent您的导航器提供自定义:

const AppTabNavigator = createMaterialTopTabNavigator(
  {
    Home: {
      screen: HomeScreen,
      navigationOptions: {
        header: null,
        tabBarVisible: true,
        activeTintColor: '#e91e63',
      },
    },
  },
  {
    tabBarComponent: CustomTabBar
  },
)
Run Code Online (Sandbox Code Playgroud)

该组件将扩展react-navigation使用的默认组件,并挂钩到一个状态。

import React from 'react'
import { MaterialTopTabBar } from 'react-navigation'

export default ({ hasMargin, ...props }) => (
  <MaterialTopTabBar
    {...props}
    showLabel
    labelStyle={{
      fontSize: 15,
      color: 'white',
    }}
    style={{
      backgroundColor: 'rgba(22, 22, 22, 0)',
      position: 'absolute',
      top: Dimensions.get('window').height-tabBarHeight,
      left: 0,
      right: 0,
      marginTop: hashMargin ? 45 : 0,
    }}
  />
)
Run Code Online (Sandbox Code Playgroud)

这里的问题是你没有变量hasMargin。您需要选择使用 Redux 的connect组件或使用 React context。两者都需要一些理解和配置,因此您可能需要阅读一些内容。完成后,定义更改上下文或分派 redux 操作。onScrollChange

  • 假设`props`对象是`{ a: 1, b: 2 }`,它会将对象的每个props赋予组件,这称为对象传播 (2认同)