I have a view similar to the following
<View style={{
flex: 1,
paddingTop: 20,
flexWrap: 'wrap',
backgroundColor: 'pink'
}}>
<View style={{
flex: 1,
minHeight: 200,
flexGrow: 1,
backgroundColor: 'red',
}} />
<View style={{
flex: 1,
minHeight: 200,
flexGrow: 1,
backgroundColor: 'blue',
}} />
</View>
The above layout works on https://yogalayout.com. Two rectangles which get wrapped if in landscape mode. (When height and width are interchanged in yogalayout. 500x300 or 300x500)
But when used in react-native the child view takes only the width of the content in it. Does not get the default alignItems: 'stretch' behavior.
If I add alignContent: 'stretch' the child views are stretched to full width in portrait mode. But, in landscape mode, the views will not be wrapped.
Just needed to use flexBasis instead of minHeight for child views along with alignContent: 'stretch' in the parent view.
Related
How can I prevent the text from making its container grow in width and instead wrap to keep the width of the container? Note that I don`t want the container to have a fixed width, the width of the container is determined by its other children.
For example, the following code
class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={[styles.bubble2, {}]}>
<View
style={{ width: 100, height: 100, backgroundColor: "#ff0000" }}
/>
<Text
style={{
fontSize: 18,
flexWrap: "wrap",
backgroundColor: "#00ff00"
}}
>
Some random text goes here
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: "#fff",
paddingTop: 20,
alignItems: "center",
justifyContent: "center"
},
bubble2: {
backgroundColor: "orange",
paddingHorizontal: 20,
alignItems: "center",
justifyContent: "center"
}
});
renders this. Note how the text makes the container width larger to fit
and what I am trying to achieve is this without setting the width of the outer container
I have:
<StyledSurface style={{ flex: 1, flexDirection: 'row', flexWrap: 'wrap' }}>
{conversations.map((c, i) => (
<Surface style={{ backgroundColor: 'red', width: '46%', margin: '2%', flexDirection: 'column', flexShrink: 1, alignSelf: 'flex-start'}} key={i}>
<Text style={{flex: 1}}>{c.title}</Text>
</Surface>
))}
</StyledSurface>
and this ends up looking like:
But I want each red box to shrink (vertically) to fit the contents and have equal spacing?
What am I missing?
On the View container add this:
alignSelf: 'flex-start',
Should do the trick
First, I can't seem to set up a CodeSandBox - the following code in the index.js file just renders a blank screen: https://codesandbox.io/s/q4qymyp2l6
import React, { Component } from 'react';
import { View } from 'react-native';
export default class JustifyContentBasics extends Component {
render() {
return (
// Try setting `justifyContent` to `center`.
// Try setting `flexDirection` to `row`.
<View style={{
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
}}>
<View style={{flex: 1, flexDirection: 'row', height: 50, backgroundColor: 'powderblue'}} />
<View style={{flex: 1, flexDirection: 'row', height: 50, backgroundColor: 'skyblue'}} />
<View style={{flex: 1, flexDirection: 'row', height: 50, backgroundColor: 'steelblue'}} />
</View>
);
}
};
But - if you copy and paste this code into the React-Native flexbox docs:
https://facebook.github.io/react-native/docs/flexbox
(first code sample under "LEARN MORE HERE") It will render three columns within a row.
I'm not understanding why. I've declared that the Views are rows with flexDirection: 'row', and they each get flex: 1, so I'd expect three equally sized rows.
With Bootstrap you can just declare a row within a container like so:
https://jsfiddle.net/HappyHands31/z0ncwe6b/
How can I achieve the same in React-Native?
You will put the contents of the code in the APP.JS file
If you give the COL value you get the result of 3 lines
flexDirection This is the direction in which the children inside the container will be
<View style={{
flex: 1,
flexDirection: 'col',//try change this line
justifyContent: 'space-between',
}}>
I am not aware of the image ratio uploaded by user upfront but I need to display all images in 4:5 ratio i.e
image width=device window width size
image height=(device window width size)*5/4
I used the above calculation for displaying my image inside container view but the image is cropped. If I use resize mode, the image is distorted.
Code:
<View style={styles.container}>
<Image style={styles.fixedRatio} source={{ uri: this.props.navigation.state.params.uri }}/>
</View>
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
fixedRatio: {
marginLeft:-10,
marginRight:-10,
backgroundColor: 'red',
flex: 1,
aspectRatio: 1,
width: deviceWidth,
height: deviceWidth*5/4
},
Edit 1:
As per #marson answer, I changed the code like below
I set background color to differentiate image. Now the images are not get cropped. But doesn't fit inside the view
<View style={{ backgroundColor: 'blue', aspectRatio:4/5, alignSelf:'center', width: '100%'}}>
<Image resizeMode={'contain'} style={{width:'100%', height:'100%'}} source={{ uri: this.props.navigation.state.params.uri }}/>
</View>
Could this work?
<View style={{
width: '100%',
aspectRatio={4.0/5.0}
}}>
<Image
style={{
width: '100%',
height: '100%',
}}
resizeMode={'contain'}
source={...}/>
</View>
I'm trying to build a layout in React-Native using Flexbox and I'm having trouble when the text is long. I want the layout to look like this:
However, when the blue text gets long, it pushes the date off of the right side of the screen like this:
I'm intentionally making the text stay on 1 line. What I want is for the blue text to expand as much as possible without making the text shrink. I'm new to RN and my work with CSS is very limited so I don't have much experience doing things like this. Here is my stylesheet code:
const styles = StyleSheet.create({
textContainer: {
flex: 1
},
separator: {
height: 1,
backgroundColor: '#dddddd'
},
title: {
fontSize: 25,
fontWeight: 'bold',
color: '#48BBEC'
},
subtitle: {
fontSize: 20,
color: '#656565'
},
dateContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end'
},
rowContainer: {
flexDirection: 'row',
padding: 10
}
});
And, finally, my layout code:
<TouchableHighlight
underlayColor='#dddddd'>
<View style={styles.rowContainer}>
<View cstyle={styles.textContainer}>
<Text numberOfLines={1} style={styles.title}>{rowData.name}</Text>
<Text style={styles.subtitle}>{rowData.teacher}</Text>
</View>
<View style={styles.dateContainer}>
<Text style={styles.subtitle}>{result}</Text>
</View>
<View style={styles.separator}/>
</View>
</TouchableHighlight>
Remove flex: 1 from dateContainer and add flexWrap: 'wrap' to textContainer.
textContainer: {
flex: 1,
flexWrap: 'wrap'
},
dateContainer: {
justifyContent: 'center',
alignItems: 'flex-end'
},
You'll have to set a width on the text component you wish to truncate (even if it's 100% of it's container which is the flexbox child I imagine), and also set numberOfLines prop to 1 (or however many lines you want to allow). See docs here on ellipsize mode and number of lines.
https://facebook.github.io/react-native/docs/text.html#ellipsizemode
Code example of what I explain above:
<View style={{display: 'flex', flexDirection: 'row', overflow: 'hidden'}}>
<View>
<Text
numberOfLines={1}
style={{width: '100%'}}>Text here that I want to truncate</Text>
<Text
numberOfLines={1}
style={{width:'100%'}}>Another line of text</Text>
</View>
<View style={{width: 120}}>
<Text>01/01/2017</Text>
</View>
</View>
If you're still having issues, it's possible you'll need to also use overflow: hidden in your text component style.
To solve your problem, remove the flex: 1 from the dateContainer and it won't go off the screen.
dateContainer: {
//flex: 1, line removed
justifyContent: 'center',
alignItems: 'flex-end'
},
Add the flex: 1 to your rowContainer as follows:
rowContainer: {
flex: 1, //Line added
flexDirection: 'row',
padding: 10
}
The solution to this is adding flexGrow: 0 to the element that is pushing others away.
I ended up solving this by measuring the parent view's width (with the onLayout callback) and then setting the width of the text's parent to the remainder.
This means having some state in the component, but it could be turned in to a general component with a left-side dynamic-sized component and a right-side static component.