【问题标题】:react-navigation How to set tabBar margin corresponding to Animated Valuereact-navigation 如何设置 Animated Value 对应的 tabBar 边距
【发布时间】:2018-12-05 00:35:07
【问题描述】:

我正在使用 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"
          }
        }
       }
      )

我也试过 marginTopthis.AnimatedHeaderValue 但似乎没有用。任何建议或 cmets 都会非常有帮助。

【问题讨论】:

  • 您能否提供一个演示该问题的代码的工作示例?调试起来会容易得多。根据您提供的信息,我只能做出推测。

标签: javascript react-native react-navigation


【解决方案1】:

您可以在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 在您的顶级 &lt;App /&gt; 中,所以更改会有点棘手,因此您可能会使用 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 操作。

【讨论】:

  • 感谢您的回复。我认为应该工作!但我不明白onScrollChange 部分。你是说我应该创建一个处理程序 onScrollChange 吗?如果是的话,里面应该有什么?
  • 它应该是组件中的一个函数,ScrollView 接收偏移量作为参数。在此函数中,您需要检查偏移量是否为 0,并修改一个变量,该变量将用于判断您的顶级视图是否应该有边距。
  • 我希望我能完全理解你。我对动画真的很陌生。如果可以,能否提供完整的代码?
  • 这不是关于动画,而是关于状态管理,这有点超出您的问题范围。我会更详细一点,但它可能会记录你自己的状态将有助于你和你对 React 的理解
  • 假设props对象是{ a: 1, b: 2 },它会将对象的每一个props赋予组件,这称为对象传播
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2020-01-25
  • 2019-08-13
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
相关资源
最近更新 更多