【问题标题】:TouchableOpacity button with an icon inside doesn't triggers in the white space内部带有图标的 TouchableOpacity 按钮不会在空白处触发
【发布时间】:2020-07-20 20:12:16
【问题描述】:

我已经制作了一个使用 TouchableOpacity 执行 ImageButton 的组件。在里面,我通常会放一个fontawesome和一个文本。问题是只有当我按下按钮的中心时才会触发按下(就在 fontawesome 上)。 另一个问题,但我认为它与第一个问题有关,如果我按下中间的图标,按下的动画不会触发。

回顾一下: 如果我不点击按钮中的图标,例如它的空白,我会看到按下动画但不会触发 onPress。

如果我点击按钮中的图标,例如字体真棒,我看不到按下动画但触发了 onPress。

组件:

import React from 'react';
import {Style} from './IconButtonStyle';
import { View, Text, TouchableOpacity,StyleSheet} from 'react-native';

const IconButton = (props) => (
    <TouchableOpacity style={!props.style?Style.button:[Style.button,props.style]} onPress={props.onPress}>
        <View>
            {props.icon}
        </View>
        <View>
            <Text>{props.title}</Text>
        </View>
    </TouchableOpacity>
)

export default IconButton;

谢谢

【问题讨论】:

    标签: react-native react-native-android expo native


    【解决方案1】:

    好的,我刚刚意识到我在搞乱调用者组件。我将 onPress 放在图像上而不是 IconButton 组件上。

    【讨论】:

      【解决方案2】:

      我已经测试了你的代码,它看起来不错:see snack

      【讨论】:

      • 请问-1是什么??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      相关资源
      最近更新 更多