I have given an ImageBackground component and I want it to take the whole screen but it appears to have small paddings even if I resize twice or more the image. Where does it comes from ?
Here is the code of the component
import React from "react";
import { View, Text, Button, ImageBackground, StyleSheet } from "react-
native";
export class HomeScreen extends React.Component {
static navigationOptions = {
//To hide the NavigationBar from current Screen
header: null
};
render() {
return (
<ImageBackground source={require('../imgs/bgx.png')} style={
styles.container }>
</ImageBackground>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'stretch',
resizeMode: 'stretch'
}
});
Make sure your source image does not have any transparency around.
Set width: '100%' and height: '100%' to the ImageBackground component. Does it change anything?
Add width='100%' and height='100%'
Related
Why and how to use the progressImage property in the <ProgressViewIOS/> component in react-native ? The documentation doesn't seem to be understandable. Can anyone share a screenshot output of this property on the iOS? Thanks !!!
progressImage is an image to use for the portion of the progress bar that is filled in ProgressViewIOS.
If you provide a custom image for it, the progressTintColor property is ignored.
Images:
progressImage.png
trackImage.png
Output:
Code:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, ProgressViewIOS, Dimensions} from 'react-native';
const screenWidth = Dimensions.get("screen").width;
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<ProgressViewIOS
style={{width: screenWidth - 30}}
progress={0.5}
progressViewStyle = 'default'
trackImage={require('./trackImage.png')}
progressImage={require('./progressImage.png')}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
This question already has an answer here:
React-Native: Width not inherited from partent view when alignItems is set to 'center'
(1 answer)
Closed 5 years ago.
Here is the code example: Expected behavior is that it should show a view with a green background. When alignItems: 'center' is set on the container, the view with a green background is not shown. Removing alignItems: 'center' from the container style shows green view. Can someone explain why it's working like that, and is that not a bug in layout system? Thanks in advance.
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={{flex: 1, backgroundColor:'green'}}>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});
It is not a bug, it is the default behavior.
alignItems default value is stretch, which makes the flex item stretch along its parent's cross axis, which for column direction is the width, the default in React Native.
So when you change that to center, it start behaves like an inline element, and collapse to its content, or else center can't center it, if it still would have full width.
By putting style alignItems as center, parent view don't provide width and inner view take width based on content. If you want fully background green. you can also set background directly on parent view and no need child view.
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'green'
}
});
How to handle with zIndex?
I tried to specify the largest number from the scrollview, but it's still at the bottom(
Without ScrollView it looks like this, but I need to scroll up to image.
Thank you
This has been implemented in iOS as of 0.30 (see the commit changes) and in android in 0.31 (changes)
I have made a simple example component for how I have been using it:
'use-strict';
import React, { Component } from 'react';
const { View, StyleSheet } = require('react-native');
const styles = StyleSheet.create({
wrapper: {
flex:1,
},
back: {
width: 100,
height: 100,
backgroundColor: 'blue',
zIndex: 0
},
front: {
position: 'absolute',
top:25,
left:25,
width: 50,
height:50,
backgroundColor: 'red',
zIndex: 1
}
});
class Layers extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View style={styles.wrapper}>
<View style={styles.back}></View>
<View style={styles.front}></View>
</View>
);
}
}
module.exports = Layers;
I want to display a navigation bar on top of the screen using React's Navigator and this custom NavigationBar component.
I am using the following code, but for some reason the navigation bar renders at the bottom of the screen:
import NavigationBar from 'react-native-navbar'
class MyDebts extends Component {
constructor(props) {
super(props)
this.state = {
selectedTab: 'persons'
}
}
_renderScene(route, navigator) {
console.log('render scene with route: ', route)
return <route.component route={route} navigator={navigator} />
}
render() {
return (
<Provider store={store}>
<TabBarIOS selectedTab={this.state.selectedTab}>
<TabBarIOS.Item
selected={this.state.selectedTab === 'persons'}
title='Persons'
onPress={() => {
this.setState({
selectedTab: 'persons',
})
}}>
<Navigator
initialRoute={{ name: 'Persons', component: PersonsRootComponent }}
renderScene={this._renderScene}
navigationBar={<NavigationBar style={styles.nav} title={{title: 'Persons'}}/>}
/>
</TabBarIOS.Item>
</TabBarIOS>
</Provider>
)
}
}
const styles = StyleSheet.create({
nav: {
height: 160,
backgroundColor: 'red'
}
})
The PersonsRootComponent renders properly, but I have no idea why the NavigationBar is placed on the bottom of the screen...
If you want to put the navbar somewhere else other than the bottom, you need absolute positioning on the navbar.
container: {
width: width,
flexDirection: 'row',
position:'absolute',
top:0,
left:0
},
I'm using Navigator component in React-Native.
//...
render() {
return (
<Navigator
ref="navigator"
navigationBar={this.renderHeader()}
sceneStyle={styles.container}
initialRoute={{type: 'Home'}}
renderScene={this.renderScene.bind(this)} />
);
}
Now I need to set a background image fixed for all scenes, but I can't wrap Navigator component inside another View.
// No errors but background is ignored
render() {
return (
<View style={styles.navigatorContainer}>
<Image source={require('image!logo-big')} style={styles.background} />
<Navigator
ref="navigator"
navigationBar={this.renderHeader()}
sceneStyle={styles.container}
initialRoute={{type: 'Home'}}
renderScene={this.renderScene.bind(this)} />
</View>
);
}
var styles = StyleSheet.create({
container: {
flex: 1
},
background: {
position: 'absolute',
left: 0,
top: 0,
width: 1024,
height: 768
},
navigatorContainer: {
flex: 1,
backgroundColor: '#FF0000'
}
});
If I set the backgroundColor to class NavigatorContainer, I can see red background but it doesn't work with image.
Any suggestions?
Found the fix: to show the background image you must set the navigatorContainer's backgroundColor to 'transparent'.