3

我用 react-navigation 中的 createMaterialTopTabNavigator 创建了两个选项卡。我喜欢为两个选项卡设置一个背景图像。

在此处输入图像描述 在此处输入图像描述

当前的行为是,当我从 tab1 滑动到 tab2 时,图像也会过渡,但我喜欢在从 tab1 过渡到 tab2 时使背景图像保持静态,并且在滑动时只有选项卡的内容才能过渡。我尝试将 TabNavigator 包装在 ImageBackground 组件中,但这没有用。

4

2 回答 2

3

我认为您可以使用以下解决方案之一:

  1. 将选项卡设置为具有透明背景的样式,并将背景图像设置在<View>导航器上方。您可以在此处的 React Navigation 文档中找到有关样式卡的详细信息。
  2. 第二种选择,我认为更优雅的选择是使用专用库来管理 React Navigation 中的转换。那里有一些,但我个人使用过Fluid Transitions,我喜欢它。如果您决定使用这个库,您可以在 StackNavigator 视图中设置背景图像。你需要添加一个shared道具,你就完成了:)
于 2020-07-06T21:23:00.070 回答
1

这是演示:https ://snack.expo.io/@nomi9995/e05080

使用react-native-tab-view并将 TabView 包装在其中的更好方法ImageBackground

yarn add react-native-tab-view
import React, { Component } from "react";
import {
  Text,
  StyleSheet,
  View,
  SafeAreaView,
  ImageBackground,
  Dimensions,
} from "react-native";
import { TabView, SceneMap } from "react-native-tab-view";
const width = Dimensions.get("window").width;

function FirstRoute() {
  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>FirstRoute!</Text>
    </View>
  );
}

function SecondRoute() {
  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>SecondRoute!</Text>
    </View>
  );
}

export default class App extends Component {
  state = {
    index: 0,
    routes: [
      { key: "first", title: "First" },
      { key: "second", title: "Second" },
    ],
  };
  render() {
    const { index, routes } = this.state;
    const renderScene = SceneMap({
      first: FirstRoute,
      second: SecondRoute,
    });
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <ImageBackground
          style={{ flex: 1, width: width }}
          source={{
            uri:
              "https://firebasestorage.googleapis.com/v0/b/ielts-preps.appspot.com/o/1592920135765?alt=media&token=ec911583-06f9-4315-b66c-cf47de120e85",
          }}
        >
          <TabView
            navigationState={{ index, routes }}
            renderScene={renderScene}
            onIndexChange={(i) => this.setState({ index: i })}
            tabBarPosition="bottom"
          />
        </ImageBackground>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({});

于 2020-07-08T20:07:55.367 回答