react-nativeimagebackground

How to Show text below ImageBackground in react native for drawer navigation?


I am making a custom drawer navigation for my app. Where I am using background image (it will not take full space), on the background image I am using another image. After that I have to show some content below background image.

This is the component I made for drawer navigation. I am not sure if this is the right approach. you can get the immediate result by pasting this code in expo app.

import React from 'react';
import { View, Text, Button, ImageBackground, Image } from 'react-native';

class Sidebar extends React.Component {
  render() {
    return (
      <ImageBackground source={{uri: 'https://images.pexels.com/photos/1738762/pexels-photo-1738762.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'}} style={{ width: '100%', height: '60%' }}>
        <Image source={{uri: 'https://facebook.github.io/react/logo-og.png'}} style={{width: 200, height: 200, marginLeft: 20}} />
          <Text style={{marginTop: 70, marginLeft: 20}}>Information</Text>
          <Text style={{marginTop: 10, marginLeft: 20}}>Settings</Text>
          <Text style={{marginTop: 10, marginLeft: 20}}>Favorite</Text>
      </ImageBackground>
    );
  }  
}

export default Sidebar

What would be the better way to get this kind of result. is this ok give as much margin to my text that it come below the background image


Solution

  • Well it not good to use margin like that. The best way is to control everything with flex inside views:

    render() {
       return (
          <View style={{flex:1}}>
          <View style={{flex:1.5, justifyContent:'center', alignItems:'center'}}>
            <ImageBackground source={{uri: 'https://images.pexels.com/photos/1738762/pexels-photo-1738762.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'}} style={{width: 100, 
            height: 100}}/>
          </View>
         <View style={{flex:3}}>  
          <View style={{flex:1, flexDirection:'row'}}>
    
              <View style={{flex:2, justifyContent: 'center', alignItems:'flex-end', backgroundColor:'blue'}}>
                  <Text >Information</Text>
              </View>
              <View style={{flex:1, backgroundColor:'red'}}>
                  <Image
                  source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
                  />
              </View>
          </View>
          <View style={{flex:1 ,flexDirection:'row'}}>
    
              <View style={{flex:2, justifyContent: 'center', alignItems:'flex-end', backgroundColor:'blue'}}>
                  <Text >Settings</Text>
              </View>
              <View style={{flex:1,}}>
                  <Image
                  source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
                  />
              </View>
          </View>
          <View style={{flex:1, flexDirection:'row'}}>
    
              <View style={{flex:2, justifyContent: 'center', alignItems:'flex-end', backgroundColor:'blue'}}>
                  <Text >Favorite</Text>
              </View>
              <View style={{flex:1}}>
                  <Image
                  source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
    
                  />
              </View>
          </View>
    
    
         </View>
    
        </View>
        );
      } 
    

    You can control your text with justifyContent, alignItems and flex in the view above your text section. You can use flex-start and flex-end or center' to displace the items in that View.

    Try to follow this link: https://facebook.github.io/react-native/docs/flexbox I hope it helps you.