Link to home
Start Free TrialLog in
Avatar of Tanya Leithoff
Tanya Leithoff

asked on

Why is top: 0 for absolute component not working in react native?

I have a SearchBar component that I need to appear over the ScrollView component towards the top of the screen (under the header). Currently, it is not snapping to the top even though I have top: 0 set on it. Does anyone know how to achieve this?

Here is the render:

render() {
    const {isLoading, products} = this.props.products;

    if (isLoading) {
        return <Loader isVisible={true}/>;
    }

    return (<View style={styles.wrapper}>
        <Header/>
        <ScrollView style={styles.scrollView}>
            <ProductsContainer data={{productsList: { results: products }}}/>
        </ScrollView>
        <SearchBar style={styles.searchBar}/>
        <Footer/>
    </View>);
}

Open in new window


Here are the styles:

var styles = StyleSheet.create({
    wrapper: {
        flex: 1,
        backgroundColor: '#000',
        position: 'relative'
    },
    searchBar: {
        position: 'absolute',
        top: 0
    },
    scrollView: {
        position: 'relative'
    }
});

Open in new window

search-bar-zindex.png
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Have you set your margins to 0 on your container?
Avatar of Tanya Leithoff
Tanya Leithoff

ASKER

I don't know which container you are talking about so I added margin: 0 to all of them but it didn't make a difference:

var styles = StyleSheet.create({
    wrapper: {
        flex: 1,
        backgroundColor: '#000',
        position: 'relative',
        margin: 0
    },
    searchBar: {
        position: 'absolute',
        top: 0,
        margin: 0
    },
    scrollView: {
        position: 'relative',
        margin: 0
    }
});

Open in new window

if you add the following to your global styles
* {
  margin: 0;
}

Open in new window

I'm not sure.  Let me check in our app.
We don't have any global styles like that.  In order to add global styles in react-native don't you have to apply the styles to every view like this?

https://stackoverflow.com/questions/30853178/react-native-global-styles
I am exploring all reasons why a gap appears at the top of the page.

I have looked at the image you posted again - where is the gap you are seeing - can you mark it on the image?
I removed the footer so that it could be easier to see.  And actually, after looking at it again now I'm not even sure that the absolute positioning is working at all, which would explain why the top: 0 style is not working.  Why would absolute positioning not work on my SearchBar in react-native?  The parent has a position of relative and so does it's sibling.  Am I missing something??
search-bar.png
I am still not with you are you saying thisUser generated image
Nevermind I was able to figure this out a different way.
Great - as a matter of interest what was your solution?
ASKER CERTIFIED SOLUTION
Avatar of Tanya Leithoff
Tanya Leithoff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok - great - well done and thanks for sharing the answer. Are you sure you still want to delete though - why not accept your last post as the answer - it might help someone who has a similar problem.
I tried accepting my own answer but it won't let me.  I just end up with a grey overall all over the screen which does nothing.
I accepted for you.