16

我正在使用react-navigation和 stack-navigator 来管理我的屏幕。

我正在使用的平台:

  • 安卓
  • 反应原生:0.47.1
  • 反应导航:1.0.0-beta.11
  • 模拟器和设备

我有一个屏幕,它充当模态表单,但它实际上是一个全屏。为什么“作为模态形式”的部分很重要?那是因为它是一种带有一些选项和透明背景颜色的模式菜单。

这是我所期望的:

在此处输入图像描述

但我得到的是:

在此处输入图像描述

如您所见,在第二个示例中,背景颜色被完全替换或先前加载的组件被卸载,因此我想要实现的效果丢失了。 这个想法是能够像任何其他屏幕一样导航到这个屏幕。

如果无法使用 react-navigation 完成,我还能采取什么其他方式来做到这一点?

该组件执行动作(redux),因为它是一个跨应用程序组件,并在内部封装了许多机制和逻辑,这就是为什么我不能将它用作使用PureComponent该组件的组件的中继。至少,将这个组件作为 PureComponent 将迫使我在许多其他组件中复制许多机制和逻辑。

为了这个问题,也为了避免问题太大,两个屏幕都有完全相同的样式,但是通过StackNavigation的那个替换了backgroundColor,或者卸载了前一个屏幕。

这是我到目前为止所拥有的:

//PlaylistSelector.js
render() {
  //Just a full size empty screen to check and avoid bugs
  //Using red instead of transparent, works

  return (
    <View style={{ flex: 1, backgroundColor: 'transparent' }}>
    </View>
  );
}

//Navigator.js
import { StackNavigator } from 'react-navigation';

import Album from './Album'; //This is the screen I expect to keep at the background
import PlaylistSelector from './PlaylistSelector';

const AppNavigator = StackNavigator(
    {
        ...moreScreens,
        Album: { screen: Album },
        PlaylistSelector: {
            screen: PlaylistSelector,
            navigationOptions: {
                style: { backgroundColor: 'red' } //Does not work, red is just to ilustrate, should be transparent,
                cardStyle: { //Does not work,
                    backgroundColor: 'transparent',
                },
                bodyStyle: { //Does not work,
                    backgroundColor: 'transparent',
                },
            }
        }
    },
    {
        initialRouteName: 'Splash',
        headerMode: 'none',
        cardStyle: { //Does not work
            backgroundColor: 'transparent',
        },
        transitionConfig: (): Object => ({ //Does not work
            containerStyle: {
                backgroundColor: 'transparent',
            },
        }),
    }
);

export default AppNavigator;
4

8 回答 8

52

这在最新React Navigation版本中确实发生了变化。看

https://reactnavigation.org/docs/themes/

例如

import * as React from 'react';
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';

const MyTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    background: 'red'
  },
};

export default function App() {
  return (
    <NavigationContainer theme={MyTheme}>{/* content */}</NavigationContainer>
  );
}
于 2020-03-22T16:01:07.603 回答
6

使用 react-navigation v3.x 你可以使用 transparentCard pro:

const MainNavigator = createStackNavigator(
  {
    BottomTabs: {
      screen: BottomTabsStack,
    },
    Modal: {
      screen: ModalScreen,
    }
  },
  {
    headerMode: 'none',
    mode: 'modal',
    transparentCard: true,
    cardStyle: { opacity: 1 } //This prop is necessary for eventually issue.
  }
);

您可以在下面找到一个完整的示例:

https://snack.expo.io/@cristiankmb/stacks-in-tabs-with-header-options-with-modal

于 2019-04-09T17:21:30.073 回答
4

react-navigation v6.x中有一个解决方案

设置Stack NavigatorcardStyle: {backgroundColor: 'transparent'}screenOptions道具对我不起作用

但是,在这个 Github issue page的帮助下,我找到了一个为我们的 NavigatorContainer 设置默认背景颜色的解决方案:

import {
  DefaultTheme,
  NavigationContainer,
} from '@react-navigation/native';

...

const navTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    background: 'transparent',
  },
};

...

return (
    <NavigationContainer
      theme={navTheme}
...

在那里我们可以改变'transparent'任何我们想要的东西。

于 2021-11-25T22:06:00.247 回答
0

添加不透明度:

cardStyle: {
  backgroundColor: "transparent",
  opacity: 1
}
于 2018-08-14T07:23:45.317 回答
0

既然你想要'Modal',你可以使用内置的'Modal' react-native。

https://facebook.github.io/react-native/docs/modal.html

于 2018-01-15T10:47:38.313 回答
0

只是抛出对我来说最直接的东西,例如设置 a headerBackgroundon screenOptions

    <Stack.Navigator
      headerMode={"float"}
      screenOptions={{
        headerBackground: () => <View style={{flex: 1, backgroundColor: themeContext.background}} />,
        backgroundColor: themeContext.background
      }}
    >

这不适用于透明标题,这实际上可能是首先引导您到这里的原因。在这种情况下,只需将整个应用程序容器包装到这样的视图中。

export default function App() {
  return (
    <View style={{flex: 1, backgroundColor: 'red'}}>
      <NavigationContainer linking={linkingConfig}>
        <StatusBar />
        <ApolloProvider client={client}>
          <Provider store={store}>
            <Navigator />
          </Provider>
        </ApolloProvider>
      </NavigationContainer>
    </View>
  )
}

显然,您想将其提取到它自己的(样式化)组件中:)

如果您使用的是 Expo,并且没有单独的深色主题,您可以简单地backgroundColor在您的app.json.

于 2021-01-21T01:40:48.607 回答
0

到目前为止,react-navigation@2.17.0有一个配置选项transparentCard可以做到这一点。

const RootNavigator = createStackNavigator(
  {
    App,
    YourModal,
  },
  {
    headerMode: 'none',
    mode: 'modal',
    transparentCard: true,
  },
);

这不会为您模糊背景;它只会让它变得透明。要正确模糊它,您需要执行类似的操作。确保从屏幕顶部边缘上方开始背景,因为卡片将从底部开始动画,并且您可能希望屏幕逐渐模糊,而不是在动画时不透明度具有锐利的边缘。

于 2018-11-05T14:36:53.473 回答
0

这种方式对我有用:

const MyDrawerNavigator = createStackNavigator(
    {
        screen1: {
            screen: screen1,
        },
        screen2: {
            screen: screen2,
        },
    },
    {
        navigationOptions: () => ({
            cardStyle: {
                backgroundColor: "rgba(0,0,0,0.5)",
            },
        }),
    }

);
于 2020-01-22T15:33:09.887 回答