가운데에 큰 버튼이있는 React-Native bottomTabNavigation
Aug 22 2020
다음과 같이 bottomTabNavigation을 만들고 싶습니다.
이미지를 넣어서 이와 같은 tabNavigation을 만들 수 position:'absolute'있었지만 이미지가 탭을 넘치고 넘쳐나는 부분은 클릭 할 수 없습니다.
이 순간 내 코드 :
<Tab.Navigator initialRouteName="Activity" tabBarOptions={{
showIcon: true,
showLabel: false,
activeTintColor: 'blue',
}}>
<Tab.Screen name="Theme" component={Themes} options={{
tabBarIcon: () => (<Image source={require('../Images/list_blue.png')} style={styles.icon}/>)
}}/>
<Tab.Screen name="Activity" component={Activity} options={{
tabBarIcon: () => (<Image source={require('../Images/idea_blue.png')} style={styles.main_icon}/>)
}}/>
<Tab.Screen name="Add" component={Add} options={{
tabBarIcon: () => (<Image source={require('../Images/plus_blue.png') style={styles.icon}/>)
}}/>
</Tab.Navigator>
//Styles
icon: {
width: 40,
height: 40,
},
main_icon: {
position: 'absolute',
bottom: -30,
width: 115,
height: 115,
}
그런 다음 소품으로 cust tabNavigation을 tabBar={props => <CustomTabBar {...props} />}만들었지 만 여전히 동일한 문제가 있습니다.
빨간색 사각형은 touchableOpacity이지만 클릭 가능한 녹색 부분 만 탭 위의 부분은 여전히 클릭 할 수 없으며 이유를 이해할 수 없습니다.
bottomTabNavigation 중간에 이렇게 큰 버튼을 만드는 방법에 대한 아이디어가 있습니까?
답변
BasvanderLinden Aug 25 2020 at 12:28
사용자 정의 탭 막대를 만들고 해당 구성 요소를 tabBar탭 탐색기 의 소품에 전달하여 작동하게 된 것 같습니다.
function CustomTabBar({ state, descriptors, navigation, position }) {
return (
<View
style={{
flexDirection: 'row',
height: 50,
alignItems: 'center',
justifyContent: 'space-around',
}}>
{state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const label =
options.tabBarLabel !== undefined
? options.tabBarLabel
: options.title !== undefined
? options.title
: route.name;
const isFocused = state.index === index;
const onPress = () => {
const event = navigation.emit({
type: 'tabPress',
target: route.key,
canPreventDefault: true,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: 'tabLongPress',
target: route.key,
});
};
const inputRange = state.routes.map((_, i) => i);
return (
<TouchableOpacity
accessibilityRole="button"
accessibilityStates={isFocused ? ['selected'] : []}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}>
{route.name === 'Screen 2' ? (
<Image
style={styles.logo}
source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }}
/>
) : (
<Image
style={styles.logo_tiny}
source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }}
/>
)}
</TouchableOpacity>
);
})}
</View>
);
}
const styles = StyleSheet.create({
logo: {
width: 80,
height: 80,
bottom: 0,
},
logo_tiny: {
width: 30,
height: 30,
},
});
그런 다음 다음과 같이 CustomTabBar를 탭 탐색기에 전달할 수 있습니다.
const App = () => {
return (
<NavigationContainer>
<Tab.Navigator
initialRouteName="Activity"
tabBarOptions={{
showIcon: true,
showLabel: false,
activeTintColor: 'blue',
}}
tabBar={(props) => <CustomTabBar {...props} />}>
<Tab.Screen name="Screen 1" component={Screen1} />
<Tab.Screen name="Screen 2" component={Screen2} />
<Tab.Screen name="Screen 3" component={Screen3} />
</Tab.Navigator>
</NavigationContainer>
);
};