ScrollView snaps back into place with { position: absolute} - react-native

Because of the many layers displayed on my form and to create transparency effects, I need to place my ScrollView at an absolute position; unfortunately, when add the { position: 'absolute' } style, my ScrollView snaps back to the top after I release my finger.
I read all the relevant threads on stackoverflow to no avail.
Here's a screen capture of the code below: http://imgur.com/a/fd4ad
Here's the code I am using:
import React, { Component } from 'react';
import { View, ScrollView, Text } from 'react-native';
class HomeTest extends Component {
render() {
const { headerTextStyle, homeView, scrollViewStyle, textStyle } = styles;
return (
<View>
<ScrollView style={scrollViewStyle} contentContainerStyle={homeView}>
<Text style={textStyle}>I'd love to figure out why this is not working.........................</Text>
</ScrollView>
<Text style={headerTextStyle}>Header</Text>
</View>
);
}
}
const styles = {
headerTextStyle: {
fontSize: 40,
alignSelf: 'center'
},
scrollViewStyle: {
position: 'absolute',
paddingTop: 60,
marginTop: 0
},
homeView: {
alignItems: 'center',
justifyContent: 'center'
},
textStyle: {
fontSize: 96
},
};
export default HomeTest;

Solution found on GitHub: https://github.com/facebook/react-native/issues/5438
Alright, the secret was to add the following styling components: (I'll have to figure out why - and post it - later and brush up my CSS skills)
Add styling to the parent view (position: relative, flex: 1)
Add new styling properties to the scroll view (top, bottom, left, right)
Here's the updated code:
import React, { Component } from 'react';
import { View, ScrollView, Text } from 'react-native';
class HomeTest extends Component {
render() {
const { headerTextStyle, homeView, scrollViewStyle, textStyle, mainView } = styles;
return (
<View style={mainView}>
<ScrollView style={scrollViewStyle} contentContainerStyle={homeView}>
<Text style={textStyle}>I'd love to figure out why this is not working.........................</Text>
</ScrollView>
<Text style={headerTextStyle}>Header</Text>
</View>
);
}
}
const styles = {
headerTextStyle: {
fontSize: 40,
alignSelf: 'center'
},
scrollViewStyle: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
paddingTop: 60
},
homeView: {
alignItems: 'center',
justifyContent: 'center'
},
textStyle: {
fontSize: 96
},
mainView: {
flex: 1,
position: 'relative'
}
};
export default HomeTest;

You just need to specify the height of scrollView
scrollViewStyle: {
position: 'absolute',
paddingTop: 60,
marginTop: 0 ,
height: 300 // <----
},

Related

How do I make TouchableOpacity only clickable within its shape on mobile?

On web, shaping a TouchableOpacity makes it so only the shape is clickable, however on mobile the entire "box" surrounding the shape is clickable. Is there any way at all to make it so just the shape is clickable?
I have tried shaping the view and setting the style on both View and TouchableOpacity to overflow: 'hidden' but this also fails to contain the area which is touchable.
import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, Text, View } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
onPress = () => {
this.setState({
count: this.state.count + 1,
});
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button} onPress={this.onPress}>
<Text> Touch Here </Text>
</TouchableOpacity>
<View style={[styles.countContainer]}>
<Text style={[styles.countText]}>
{this.state.count !== 0 ? this.state.count : null}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 10,
overflow: 'hidden',
},
button: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
borderRadius: 640,
height: 320,
width: 320,
overflow: 'hidden',
},
countContainer: {
alignItems: 'center',
padding: 10,
},
countText: {
color: '#FF00FF',
},
});
Here's a snack example of my problem

center text in a view on top of the screen (react-native)

I am trying to create a view with height of 200 and with some background color. on top of the view i want to place two texts in the center of the screen right below the toolbar. my code is not centering the texts. texts are appearing on the left side but i want them to be centered.
I am using Base.js style script as a way to standardize my application to reuse styles
see attached image
see code below
//Base.js
import {StyleSheet, Dimensions} from 'react-native'
export const dimensions = {
fullHeight: Dimensions.get('window').height,
fullWidth: Dimensions.get('window').width
}
export const colors = {
primary: '#039BE5',
secondary: '#027CB7',
tertiary: '#FF4081'
}
export const padding = {
sm: 10,
md: 20,
lg: 30,
xl: 40
}
export const margin = {
margin_1dp: 1,
margin_4dp: 4,
}
export const fonts = {
font_18sp: 18,
font_44sp: 44,
lg: 28,
//primary: 'Cochin'
}
export const pos = {
position: 'absolute',
top: 0,
alignItems: 'center',
justifyContent:'center',
}
// added below our colors, fonts, padding etc
// base styles
const baseStyles = {
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'stretch',
},
header: {
backgroundColor: 'transparent',
position: pos.position,
top: 0,
fontSize: fonts.font_44sp,
fontFamily: fonts.primary,
fontWeight: 'bold',
color: 'black',
textAlign: 'center',
},
sub_header: {
backgroundColor: 'transparent',
position: pos.position,
top: 55,
fontSize: fonts.font_18sp,
fontFamily: fonts.primary,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
},
image: {
height: 200, justifyContent:'center', backgroundColor: '#00BCD4'
},
section: {
paddingVertical: padding.lg,
paddingHorizontal: padding.xl
},
}
export default function createStyles(overrides = {}) {
return StyleSheet.create({...baseStyles, ...overrides})
}
//This is an example code for NavigationDrawer//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text } from 'react-native';
// import all basic components
import createStyles from '../styles/Base.js'
const styles = createStyles()
//import styles from './Style.js'
export default class Screen1 extends Component {
//Screen1 Component
render() {
return (
<View style={styles.container}>
<View style={{ height: 200, justifyContent:'center', backgroundColor: '#00BCD4' }} />
<Text style={styles.header}> $0.00 </Text>
<Text style={styles.sub_header}> Current Balance </Text>
</View>
);
}
texts should be center in the middle of the screen and below the toolbar.
can anyone help fix my code so that the texts are center? thanks in advance
The default flexDirection in react-native is column ... so you don need to specify it ... and alignItems will center children horizontally with column-flex-direction
<View
style={{ alignItems: 'center', backgroundColor: '#00BCD4', height: 200 }}
>
<Text style={styles.header}> $0.00 </Text>
<Text style={styles.sub_header}> Current Balance </Text>
</View>
So this might not solve everything but a common mistake people make when it comes to flexbox is they don't realize when they specify a flex-direction, the styles you use to center get reversed.
Typically if flex-direction was set to default which is row, you would use align-items to vertically center.
However, because you have it set to column, you need to use justify-content instead.
This is a pretty good article on flex. https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Native Base button with margin in style does not the text inside it

I wrote an app on react native using native base. when I try to put margin in button it will not show the text inside the button. How can I fix it?
When I delete the margin it work fine. but when i put the margin back it do not work. I search many source but no one show any solution at all.
Here is the code:
import React, { Component } from 'react';
import { Platform, StyleSheet, View, Image } from 'react-native';
import FloatLabelInput from './src/components/FloatLabelInput';
import {Button, Text} from 'native-base'
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Image
style={styles.background_Image}
source={require('./media/free-blurry-background-1636594.jpg')}
/>
<Image source={require('./media/Save-the-Children-Logo.png')}
style={{height: '10%', width: '100%'}}/>
<FloatLabelInput name='Username' security='false'/>
<FloatLabelInput name='Password' security='true'/>
<Button block rounded danger
onPress={() => console.log('my first app')}
style = {styles.button_style}
// accessibilityLabel="Learn more about this purple button"
>
<Text >LOGIN</Text>
</Button>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#C0C0C0',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
background_Image: {
backgroundColor: '#ccc',
flex: 1,
position: 'absolute',
width: '100%',
height: '100%',
justifyContent: 'center',
},
button_style: {
margin: '5%'
}
});
I believe this is a bug related to percentage by referring to this issue https://github.com/facebook/react-native/issues/19164.. For alternative solution try using px
button_style: {
margin: 5
}

Image becoming transparent when placed within view

My app has a background image which has a slightly transparent view placed over it to provide a blue tint to the background image. I am now trying to place the app logo on top of these views but when I do so the logo appears to take on the opacity of the view providing the tint.
I am very new to react native but what I basically need is to have: background image > view > logo.
To demonstrate what I mean, the app logo should look like this:
But it currently looks washed out, like this:
App.js
import React, { Component } from 'react';
import { View, Text, ImageBackground } from 'react-native';
import Logo from './Logo';
class App extends Component {
render() {
return (
<ImageBackground
source={require('./images/city.png')}
style={styles.backgroundStyle}
>
<View style={styles.backgroundOverlayStyle}>
<Logo />
</View>
</ImageBackground>
);
}
}
const styles = {
backgroundStyle: {
flex: 1,
backgroundColor: '#000000',
width: '100%',
height: '100%'
},
backgroundOverlayStyle: {
flex: 1,
backgroundColor: '#003284',
opacity: 0.5
}
};
export default App;
Logo.js
import React from 'react';
import { View, Image } from 'react-native';
const Logo = () => {
const { logoStyle } = styles;
return (
<View style={styles.container}>
<Image style={logoStyle} source={require('./images/request-first-logo-white.png')} />
</View>
);
};
const styles = {
container: {
alignItems: 'center',
justifyContent: 'center',
opacity: 1,
},
logoStyle: {
width: '70%',
height: '65%',
resizeMode: 'contain',
//backgroundColor: 'blue'
}
};
export default Logo;
Thank you for any help.
you need you define blurRadius for ImageBackground
import React, { Component } from 'react';
import { View, Text, ImageBackground,Image } from 'react-native';
const Logo = () => {
const { logoStyle } = styles;
return (
<View style={styles.container}>
<Image style={logoStyle} source={{uri:'https://www.what-dog.net/Images/faces2/scroll001.jpg'}} />
</View>
);
};
export default class App extends Component {
render() {
return (
<ImageBackground
source={{uri : 'https://pngstocks.com/wp-content/uploads/2018/03/cb-background-10.jpeg'}}
style={styles.backgroundStyle}
blurRadius={30}
>
<Logo />
</ImageBackground>
);
}
}
const styles = {
backgroundStyle: {
flex: 1,
backgroundColor: '#000000',
width: '100%',
height: '100%'
},
backgroundOverlayStyle: {
flex: 1,
backgroundColor: '#003284',
},
container: {
alignItems: 'center',
justifyContent: 'center',
},
logoStyle: {
width: 100,
height: 100,
resizeMode: 'contain',
//backgroundColor: 'blue'
}
};

Custom React Native component doesn't animate

So I've created a custom component in React Native, which just wraps a TouchableOpacity and turns it into a nice button component. Though when I use an animated version of the component (through createAnimatedComponent) it does not move, anyone know why?
const AnimatedMainButton = Animated.createAnimatedComponent(MainButton);
...
render() {
return <AnimatedMainButton style={{transform: [{translateX: 100}]}} />
}
...
import React, { Component } from 'react';
import {
Text,
View,
StyleSheet,
TouchableOpacity,
} from 'react-native';
export default class MainButton extends Component {
static propTypes = {
title: React.PropTypes.string.isRequired,
onPress: React.PropTypes.func.isRequired,
color: React.PropTypes.string,
textColor: React.PropTypes.string,
};
static defaultProps = {
color: '#FFFFFF',
textColor: '#000000',
};
render() {
const { title, onPress, color, textColor } = this.props;
return (
<TouchableOpacity style={[styles.mainbutton, {backgroundColor: color}]} onPress={onPress}>
<Text style={[styles.mainbuttontext, {color: textColor}]}>{title}</Text>
</TouchableOpacity>
)
}
}
const styles = StyleSheet.create({
mainbutton: {
width: '80%',
height: 60,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 10,
shadowColor: '#000000',
shadowOffset: { width: 0, height: 10 },
shadowOpacity: 0.8,
shadowRadius: 20,
elevation: 5,
},
mainbuttontext: {
fontSize: 40,
fontFamily: 'DroidSerif',
},
});
You need to pass down the style to the elements contained within the custom component. duh! Otherwise any transformations done by AnimatedComponent, or parents of, won't reach the native base components.
return (
<TouchableOpacity style={[this.props.style, ...