React Native Styling: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 68: Line 68:
You can then change the height to fit the image<br>
You can then change the height to fit the image<br>
[[File:Stretch with correct height.png|200px]]
[[File:Stretch with correct height.png|200px]]
<br>
<syntaxhighlight lang="jsx">
<syntaxhighlight lang="jsx">
     image: {
     image: {

Revision as of 01:10, 15 December 2020

Overview

Resources

Go to https://github.com/danielstern/styling-react-native. Really like Daniel.

Stylesheet.create()

Camel Case is used and an object created. There are no px suffixes for pixel values. Should work on all devices. The names do not necessary align with Android or IoS name. Only works on React components and not standard tags like div

const styles = Stylesheet.create({
   container: {
      backgroundColor: "white",
      height: 470.
   }
})


The snippet for this is rnss

Fixed Dimension

A brief example of fixed dimensions shows how to build a POC for a specified device. Whilst no good for release it is a lot quicker to build for one offs.

Images

Images need either a width of a height to show in react native. Without no image is shown. In the course they used this. I do like the hairlineWidth

...
export const Logo = () => (
    <Image source={img} style={styles.logo} />
)

const styles = StyleSheet.create({
    logo: {
        height: 110,
        borderBottomColor: 'darksteelblue',
        borderBottomWidth: StyleSheet.hairlineWidth
    }
});

Flex Box

Introduction

flex: 0 auto;

This is the same as flex: initial; and the shorthand for the default value: flex: 0 1 auto. It sizes the item based on its width/height properties (or its content if not set).

It makes the flex item inflexible when there is some free space left, but allows it to shrink to its minimum when there is not enough space. The alignment abilities or auto margins can be used to align flex items along the main axis.

flex: auto;

This is equivalent to flex: 1 1 auto. Beware, this is not the default value. It sizes the item based on its width/height properties, but makes it fully flexible so that they absorb any extra space along the main axis.

If all items are either flex: auto, flex: initial, or flex: none, any remaining space after the items have been sized will be distributed evenly to the items with flex: auto.

flex: none;

This is equivalent to flex: 0 0 auto. It sizes the item according to its width/height properties, but makes it fully inflexible.

This is similar to flex: initial except it is not allowed to shrink, even in an overflow situation.

flex: <positive-number>

Equivalent to flex: 1 0px. It makes the flex item flexible and sets the flex basis to zero, resulting in an item that receives the specified proportion of the remaining space.

If all items in the flex container use this pattern, their sizes will be proportional to the specified flex factor.

Approach

First approach was to create style for the app and surround the existing approach with views and adding flex 1 to each component to make sure it takes up the remaining space. For the padding values, they were not changed.
Adding flex values of 1 and 15 means the component with 15 will be 15 times bigger than the 1, so a footer and body might use this approach So

  • Replace the specified dimensions
  • Replace the font size with vh
  • Images see below

Images

  • Do not use pixels values
  • Generally you give it a height or a width using viewport e.g. vh for height
  • Use alignItems stretch to fit the container

Images

Specifying the height or the width means that the other axis will stretch the image in proportion to fill the width. In this case the height is too small but the with is stretch to fill the container.
You can then change the height to fit the image

<syntaxhighlight lang="jsx">

   image: {
       // height: 200,
       //width: 200,
       height:'32vh',
       display: 'flex',
       alignItems: 'stretch'
   },

</syntaxhighlight