React Native-TouchableOpacity在位置为绝对的容器上不起作用

时间:2020-06-07 00:07:15

标签: javascript css reactjs react-native

我有一个动画的 SafeAreaView ,其中有2个按钮,该视图具有 位置:绝对 。 当我单击按钮时,它会通过它,并击中de button后面的元素。 我目前正在使用zIndex和height,并尝试了许多在Stack Overflow上找到的解决方案,例如:

  • 使用TouchableHighlight
  • 使用onPressIn代替onPress
  • 从react-native-gesture-handler而不是react-native导入TouchableOpacity

如果我在父容器中使用 position:relative ,该按钮将起作用。

这是我的代码, Button 组件的样式为TouchableOpacity,我还尝试了删除包装器,将主容器更改为TouchableOpacity而不是SafeAreaView,但一无所获。 ..

return (
    <Wrapper
      isAndroid={Platform.OS === 'android'}
      style={{
        transform: [
          {
            translateY: translateValue.interpolate({
              inputRange: [0, 1],
              outputRange: [500, 0]
            })
          }
        ]
      }}>
      <ButtonsWrapper>
        <ActionButton
          inverted
          secondary
          onPress={() => {
            console.log('teste');
          }}
          accessible
          accessibilityLabel="Continuar com Facebook">
          <Icon name="edit" secondary size={20} />
          <BtnText bold noPadding secondary>
            Editar
          </BtnText>
        </ActionButton>
        <ActionButton
          inverted
          primary
          onPress={() => {
            console.log('teste');
          }}
          accessible
          accessibilityLabel="Continuar com Facebook">
          <Icon name="x-circle" primary size={20} />
          <BtnText bold noPadding primary>
            Encerrar
          </BtnText>
        </ActionButton>
      </ButtonsWrapper>
    </Wrapper>
  );
});

const Wrapper = Animated.createAnimatedComponent(styled.SafeAreaView`
  width: 100%;
  border-top-width: 1px;
  border-top-color: ${({ theme: { border } }) => border};
  background: ${({ theme: { background } }) => background};
  z-index: 1;
  position: absolute;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  bottom: 0;
  left: 0;
  ${({ isAndroid }) => isAndroid && 'elevation: 1'}
  height: 150px;
`);

const ButtonsWrapper = styled.View`
  flex: 1;
  align-items: center;
  justify-content: center;
  padding: 10px;
`;

const ActionButton = styled(Button)``;

const BtnText = styled(Text)`
  padding-right: 20px;
  flex: 1;
  text-align: center;

  ${({ noPadding }) =>
    noPadding &&
    `
      padding: 0px;
  `}
`;

1 个答案:

答案 0 :(得分:1)

我刚刚发现了问题,在App.js中,我只是更改了组件的顺序。 警报 组件放置在de NavigationContainer 之前,并放置在 NavigationContainer 之后> 它按预期工作。

之前:

export const App = () => (
  <>
    <StatusBar backgroundColor={theme.background} barStyle="dark-content" />
    <ThemeProvider theme={theme}>
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <Alert />
          <Socket>
            <NavigationContainer>
              {...}
            </NavigationContainer>            
          </Socket>
        </PersistGate>
      </Provider>
    </ThemeProvider>
  </>
);

之后:

export const App = () => (
  <>
    <StatusBar backgroundColor={theme.background} barStyle="dark-content" />
    <ThemeProvider theme={theme}>
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>          
          <Socket>
            <NavigationContainer>
              {...}
            </NavigationContainer>            
            <Alert />
          </Socket>
        </PersistGate>
      </Provider>
    </ThemeProvider>
  </>
);

就是这样:)