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

时间:2018-12-05 00:35:08

标签: javascript react-native react-navigation

我正在使用反应导航Tab Navigation。我的标签导航上方有一个标头,它可以折叠和展开,与scrollView相对应。

这是我的问题:当我一直向上滚动时,标题将折叠,这就是我想要的,但是tabBar将保持静态(请参见照片)。有没有办法设置与滚动视图相对应的tabBar margin?这样,在标题折叠时就不会marginTop

enter image description here

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"
          }
        }
       }
      )

我也尝试了marginTopthis.AnimatedHeaderValue,但似乎没有用。任何建议或评论都将非常有帮助。

1 个答案:

答案 0 :(得分:3)

您可以在Animated.event上添加一个listener,这将使您获得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>

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

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


由于您需要在运行时更改tabBarOptions,因此需要为导航器创建并提供自定义tabBarComponent

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

此组件将扩展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,
    }}
  />
)

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