Set Button in Position "Fixed" in React Native

Adding a UI element such as a button on fixed position on react native modal

As you noted yourself, Content exceeds screen height. Since FAB is it's child, it is positioned relative to the total size of Content, not the size of the screen. You should move the FAB up in your view hierarchy:

{...}
<Text>Some Text Here</Text>
</Content>
<FAB
style={styles.fab}
small
icon="plus"
onPress={() => console.log('Pressed')}
/>
</Container>
{...}

How to fixed element in react native

If you are looking for Floating Action Button like a solution with Flatlist like below example image, find the full code here.

Example Image

<FlatList
data={data}
renderItem={({ item }) => <View style={styles.list}>
<Text>Name : {item.name}</Text>
<Text>Age : {item.age}</Text>
</View>}
/>

<TouchableOpacity onPress={() => alert('FAB clicked')}
style={{
position: 'absolute',
width: 56,
height: 56,
alignItems: 'center',
justifyContent: 'center',
right: 20,
bottom: 20,
backgroundColor: '#03A9F4',
borderRadius: 30,
elevation: 8
}}>
<Text style={{ fontSize: 40,color: 'white'}}>+</Text>
</TouchableOpacity>

How to set fixed position the react native action button on the screen

You'll want to use the Animated API

First you need to create a Flatlist that you can animate. This is easily done with Animated.createAnimatedComponent():

import { Flatlist, Animated } from 'react-native'

const AnimatedFlatlist = Animated.createAnimatedComponent(Flatlist)

Now in your parent component constructor create a new Animated.Value. This will be the value you use to animate your component to scroll along with your AnimatedFlatlist:

constructor(props) {
super(props)
this.state = { translateY: new Animated.Value(0)}
}

Then wrap the component you want to be fixed in an Animated.View and make sure you add a transform to the component style, passing it the animated value you created :

<Animated.View style={{ transform: [{ translateY: this.state.translateY }]}}>
{YourFixedComponent}
</Animated.View>

Finally, you need to attach an Animated.event to your AnimatedFlatlist. This is done using the onScroll prop:

<AnimatedFlatlist
data={...}
renderItem={...}
keyExtractor={...}

onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.translateY}}}],
{useNativeDriver: true}
)}
scrollEventThrottle={1}
/>

Now your element with move along with your Flatlist. This is a basic example, and you will probably need to mess with interpolation and CSS positioning to get it exactly as you want it.

How do I fix the position of a button in typescript react

So this was just a plain CSS issue, this is what I did:

<div style={{position:"relative"}}>
<div style={{position:"absolute", float: "right", bottom:-1170, right:0}}>
<Button
key="copy-to-clipboard"
id="copy-to-clipboard"
text="Copy"
icon="file_copy"
width={120}
height={40}
borderRadius={"3px"}
onClick={copyToClipboard}
margin="5px 10px"
/>
</div>
</div>

Fixed button over a scrollview

Ok i got it :

render() {
return (
<View>
<Header />
<ScrollView>
<View>
<ItemSquareDisplay
items={this.props.items && this.props.items.length > 0
? this.props.items
: ''} />
</View>
</ScrollView>
<Button style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>+</Text>
</Button>
</View>
);
}

Quite obviously, I needed to extract my button from my Container component.
Thanks for your answers guys.



Related Topics



Leave a reply



Submit