Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full screen image in React Native

How do you make an <Image> fill the entire UIWindow sized area in React Native? If I were using Autolayout I would set a constraint on each edge, but flux is very different paradigm and I'm not a web guy. Without setting a manual width/height on my <Image> nothing shows up, but how do I dynamically tell the style to be the same as the width and heigh of its parent element, or at the very least the window?

like image 281
Jarsen Avatar asked Mar 30 '15 15:03

Jarsen


People also ask

How do you full screen in react-native?

In your project's android manifest file, select fullScreen activity. Show activity on this post. Show activity on this post.

How do I make an image full width react-native?

To fit an Image to the full width of the screen and maintain aspect ratio with React Native, we can set the width to null and the height to the height we want. to set the height of the Image to 300 and the width to null to fit the Image to make the width flexible and maintain aspect ratio.


1 Answers

You need to use flexbox. Here's a full example:

'use strict';

var React = require('react-native');

var {
  AppRegistry,
  StyleSheet,
  View,
  Image
} = React;

var TestCmp = React.createClass({
  render: function() {
    return (
      <View style={styles.imageContainer}>
        <Image style={styles.image} source={{uri: 'http://lorempixel.com/200/400/sports/5/'}} />
      </View>
    );
  }
});

var styles = StyleSheet.create({
  imageContainer: {
    flex: 1,
    alignItems: 'stretch'
  },
  image: {
    flex: 1
  }
});

AppRegistry.registerComponent('RCTTest', () => TestCmp);

Notice that you need a container to allow you to define the flex of items within it. The key here is alignItems: 'stretch' to make the contents of imageContainer fill the available space.

iOS Simulator Screenshot

like image 199
Colin Ramsay Avatar answered Nov 02 '22 14:11

Colin Ramsay