如何在RNRF中使自定义导航栏透明

时间:2017-08-02 05:01:38

标签: javascript reactjs react-native react-navigation react-native-router-flux

我已尝试使用react-native-router-flux中的所有选项:navigationBarStylenavTransparent以及 navigationBarStyle={[STYLES.navBar]} 所有这些都没有工作,同时试图使自定义导航栏透明是否有任何方法使反应导航使其透明,我应该从react-native-router-flux更改为react-navigation来做到这一点我发布了它作为Git Hub中的一个问题 这是链接:Github issue #2132

因为我发现很难使用反应导航我已经切换到RNRF有没有任何解决方案使自定义navBar透明而不是从RNRF切换到RN或者它是RN中的错误

this is what my navBar looks like now

这是我在NavBar.js文件中使用的代码:

import {
 View, Image, StatusBar, TouchableWithoutFeedback
} from 'react-native';
import React, { Component } from 'react';
import { Actions } from 'react-native-router-flux';

class NavBar extends Component {
  render() {
    return (
      <View style={styles.backgroundStyle}>
      <StatusBar />
      <View style={{ flexDirection: 'row' }}>
      <TouchableWithoutFeedback onPress={() => Actions.pop()}>
      <Image
    source={require('./Images/back-arrow.png')}
    style={styles.backarrowStyle} />
      </TouchableWithoutFeedback>

      <Image
  source={require('./Images/help.png')}
  style={styles.helpStyle} />


  <Image
source={require('./Images/setting.png')}
style={styles.settingStyle} />
    </View>
</View>
    );
  }

}
const styles = {
  backgroundStyle: {
 backgroundColor: 'transparent',
  },
  backarrowStyle: {
    resizeMode: 'contain',
    flexDirection: 'row',
    width: 55,
    height: 55,
    left: 0,
    justifyContent: 'flex-start'
  },
  helpStyle: {
    resizeMode: 'contain',
      width: 50,
      height: 50,
      left: 210,
      justifyContent: 'flex-end',
      position: 'relative'

  },
  settingStyle: {
    resizeMode: 'contain',
    width: 50,
    height: 50,
    justifyContent: 'flex-end',
  position: 'relative',
  left: 210
}
};


export default NavBar;

这是我的Router.js文件:

import React from 'react';
import { Scene, Router } from 'react-native-router-flux';
import MainScreen from './components/MainScreen';
import ChallengeScreen from './components/ChallengeScreen';
import NavBar from './components/NavBar';
import Toss from './components/Toss';

const RouterComponent = () => {
  return (
    <Router>
<Scene key="root">
    <Scene key="homeScreen" component={MainScreen} hideNavBar={1} />
    <Scene
     key="screen2" component={ChallengeScreen}
     navBar={NavBar}
        />
    <Scene
     key="screen3" component={Toss}
   navBar={NavBar}
        />
</Scene>
    </Router>
  );
};
export default RouterComponent;

1 个答案:

答案 0 :(得分:1)

您可以通过在navigationBarStyle中设置导航栏不透明度来设置半透明导航栏

<Scene key="scene2" component={ChallengeScreen} 
       navigationBarStyle={{opacity:0.3}}/> 

但问题是整个导航栏包括左右按钮都会继承不透明度。我建议只设置hidenavbar = {true}并在场景组件中实现自定义导航栏。例如:在Scene2组件(ChallengeScreen)中

 render() {
     const customNavbar = {
       <View style={{position:'absolute', top:0, flexDirection:'row', justifyContent:'space-between', backgroundColor:'transparent', padding:10}}>
           <TouchableOpacity>
            <Text>Left Button</Text>
           </TouchableOpacity>
           <Text>Title</Text>
           <TouchableOpacity>
              <Text>Right Button</Text>
           </TouchableOpacity>
       </View>
    }

   return () {
     <View style={{flex:1}}>
         {customNavbar}
         <View></View>
     </View>
   }
 }