I have looked at most of the question related to mine but haven't found a solution, my code looks as follows:
<View style={{flex: 1}}>
<ScrollView style={{flexGrow: 1}}>
<Component />
<AnotherComponent />
<View style={{flex: 1}}>
<ScrollView
style={{}}
nestedScrollEnabled
horizontal={true}>
<Button />
<Button />
<Button />
</ScrollView>
</View>
</ScrollView>
</View>
When I remove the horizontal attribute and add a specific height to the nested ScrollView than the scroll will work vertically but the moment I set horizontal={true}, the Buttons disappear.
Drawing of the layout:
I created a Custom Buttom for design purpose which look like this
Check this Snack too
import * as React from 'react';
import {
Text,
View,
StyleSheet,
ScrollView,
Button as BTN,
Dimensions,
} from 'react-native';
function Button({ title, style = {} }) {
return (
<View style={{ ...style }}>
<BTN title={title} />
</View>
);
}
export default Button;
And then in the nested scrolls look like this
import Button from './components/Button';
// Watch out here I have imported my custom button and not the Button from "react-native"
<View style={{ flex: 1 }}>
<ScrollView style={{ flexGrow: 1 }}>
<View></View>
<View></View>
<View style={{ flex: 1 }}>
<ScrollView
style={{ flexDirection: 'row' }}
nestedScrollEnabled
horizontal={true}>
<Button title="TEST" style={{ width: 100, margin: 10 }} />
<Button title="TEST" style={{ width: 100, margin: 10 }} />
<Button title="TEST" style={{ width: 100, margin: 10 }} />
</ScrollView>
</View>
</ScrollView>
</View>
Related
I am using Native Base tabs as following :
<ImageBackground
source={{uri:imageURl}}
style={{ flex: 1 }}
resizeMode="cover">
<Tabs tabBarUnderlineStyle={{ backgroundColor: "#000000" }}
style={{backgroundColor:'transparent'}}>
<Tab heading={'Tab 1'}>
<View style={{flex:1,backgroundColor:'transparent'}}>
<Text>Tab 1 content</Text>
</View>
</Tab>
</Tabs>
<Tabs tabBarUnderlineStyle={{ backgroundColor: "#000000" }}>
<Tab heading={'Tab 2'}>
<View style={{flex:1,backgroundColor:'transparent'}}>
<Text>Tab 2 content</Text>
</View>
</Tab>
</Tabs>
</ImageBackground>
The content of the Tab has a transparent background so it should be an image as the parent Image background but it has a white color , when i changed the transparent in the View inside the tab to red it changed ! i also tried removing the tabs and adding a text instead i saw the background normally.
the question is:how to make the content of the Tab transparent not white.
here is the example on snack: Native Base Tabs
I'm not sure if this is what you want?
import * as React from 'react';
import { Text, View, StyleSheet,ImageBackground } from 'react-native';
import { Tab, Tabs } from 'native-base';
export default function App() {
const imageUrl={ uri: "https://reactjs.org/logo-og.png" };
return (
<View style={styles.container}>
<ImageBackground
source={imageUrl}
style={styles.image}
>
<Tabs tabBarUnderlineStyle={{ backgroundColor: "transparent" }}>
<Tab heading={'Tab 1'} style={{flex:1,backgroundColor:'transparent'}} >
<View style={{flex:1,backgroundColor:'transparent'}}>
<Text style={{color:"white"}}>Tab 1 content</Text>
</View>
</Tab>
<Tab heading={'Tab 2'} style={{flex:1,backgroundColor:'transparent'}}>
<View style={{flex:1,backgroundColor:'transparent'}}>
<Text style={{color:"white"}}>Tab 2 content</Text>
</View>
</Tab>
</Tabs>
</ImageBackground>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column"
},
image: {
flex: 1,
resizeMode: "cover",
justifyContent: "center"
}
});
I want to show a modal when the user taps a button, without dismissing the keyboard. Unfortunately, the keyboard is dismissed as soon as the modal appears.
Minimum repro case:
import * as React from "react";
import { Button, Modal, Text, TextInput, View } from "react-native";
function TestComp() {
const [showingModal, setshowingModal] = React.useState(false);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", marginTop: 22 }}>
<Modal visible={showingModal} transparent onRequestClose={() => setshowingModal(false)}>
<View style={{ flex: 1, marginTop: 22 }}>
<Button title={"hide modal"} onPress={() => setshowingModal(false)} />
</View>
</Modal>
<TextInput placeholder="Focus to show keyboard" />
<Button title={"Show modal"} onPress={() => setshowingModal(true)} />
</View>
);
}
FYI, I am using expo.
How can I force the keyboard to persist?
You can add a hidden TextInput inside the modal with the attribute autoFocus, it's a pretty simple workaround, when you press the button and the modal showsup the focus will go to the hidden input keeping the keyboard open
https://snack.expo.io/Q01r_WD2A
import * as React from 'react';
import {Button,Modal,Text,TextInput,View,Keyboard,ScrollView} from 'react-native';
export default function TestComp() {
const [showingModal, setshowingModal] = React.useState(false);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", marginTop: 22 }}>
<Modal
visible={showingModal}
transparent
onRequestClose={() => setshowingModal(false)}>
<View style={{ flex: 1, marginTop: 22 }}>
<TextInput autoFocus={true} placeholder="Autofocus to keep the keyboard" style={{display: 'none'}} />
<Button title={'hide modal'} onPress={() => setshowingModal(false)} />
</View>
</Modal>
<TextInput placeholder="Focus to show keyboard" />
<Button title={'Show modal'} onPress={() => setshowingModal(true)} />
</View>
);
}
I wanted to add space between button but it's not working below is my code. What do I need to add in my code?
render(){
return (
<View>
<Text>Home</Text>
<Button style={styles.button}
title='Add '
/>
<Button style={styles.button}
title='Search/Update'
/>
</View>
)
}
}
const styles = StyleSheet.create({
button: {
marginBottom: 20,
padding: 30
},
})
Easiest way is to add a spacing view between your two buttons:
<View>
<Text>Home</Text>
<Button style={styles.button} title='Add' />
<View style={styles.space} /> // <------------------------- right here
<Button style={styles.button} title='Search/Update' />
</View>
const styles = StyleSheet.create({
button: {
marginBottom: 20,
padding: 30
},
space: {
width: 20, // or whatever size you need
height: 20,
},
})
Or you could use like this:
<View>
<View style={{ flex: 1, marginBottom: 10 }}>
<Button title="Add" />
</View>
<View style={{ flex: 1 }}>
<Button title="Search/Update" />
</View>
</View>
You can apply this styling to View component in order to get space between two buttons.
Also import Dimensions from react-native and adjust the width of View according to your need.
import { Dimensions } from 'react-native';
<View style={{
width:Dimensions.get("window").width * 0.5,
display:"flex",
justifyContent:"space-evenly",
alignItems: "center"
}}>
<Text>Home</Text>
<Button
style={styles.button}
title='Add '
/>
<Button
style={styles.button}
title='Search/Update'
/>
</View>
The code <View style={{marginVertical: 10}} You have to use here.
The Button should come inside View Tag
You can use the below code to add space between button
<View style={{marginVertical: 10}}>
<Button title="Button 1" />
</View>
<View style={{marginVertical: 10}}>
<Button title="Button 2" />
</View>
I have buttons that are not located at the same place in iOS and Android.
In Android they are at the middle bottom of the screen (it is what I expect) but in iOS they are at the top of the screen.
Do you know how I can put buttons in iOS at the same place as Android ?
import { Button } from 'react-native-elements';
...
<ImageBackground
key={`image${i}`}
source={{uri: image}}
style={{ width: deviceWidth, justifyContent:'center', alignItems: 'center', flex: 1 }}>
<View style={{flex: 1}}></View>
<Text style={{flex: 1}}> {textToDisplayEnglish[i]} </Text>
<Button
title={textA[i]}
style={{flex: 1}}
type={typeButton[i]}
onPress={() => this.triggerAction(i)}
/>
<View style={{flex: 1}}></View>
</ImageBackground>
you have too many flex:1. It'd be best to avoid using too many of them,unless you need to. try something like this:
import { Button } from 'react-native-elements';
...
<ImageBackground
key={`image${i}`}
source={{uri: image}}
style={{ width: deviceWidth, justifyContent:'center', alignItems: 'center', flex: 1 }}>
<Text> {textToDisplayEnglish[i]} </Text>
<Button
title={textA[i]}
type={typeButton[i]}
onPress={() => this.triggerAction(i)}
/>
</ImageBackground>
hope it helps!
I have two buttons that look like this
This is the code
render = () => (
<Image
source={require('../../images/login.jpg')}
style={[AppStyles.containerCentered, AppStyles.container, styles.background]}
>
<Image
source={require('../../images/logo.png')}
style={[styles.logo]}
/>
<Spacer size={200} />
<View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
<View style={[AppStyles.flex1]}>
<Button
title={'Login'}
icon={{ name: 'lock' }}
onPress={Actions.login}
/>
</View>
</View>
<Spacer size={10} />
<View style={[AppStyles.row, AppStyles.paddingHorizontal]}>
<View style={[AppStyles.flex1]}>
<Button
title={'Sign up'}
backgroundColor={'#FB6567'}
icon={{ name: 'face' }}
onPress={Actions.signUp}
/>
</View>
</View>
</Image>
)
I want the buttons to occupy one row and possibly share 40% - 40% of the space with the rest of the 20% going to the padding. How can i have them occupy one row?.
You'd need to define a container with flexDirection set to row and use justifyContent depending on where you want your padding:
render() {
return (
<View style={styles.container}>
<View style={styles.button} />
<View style={styles.button} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between'
},
button: {
backgroundColor: 'green',
width: '40%',
height: 40
}
});
Change space-between to space-around if you want the padding to be distributed to the sides too. (Demo)