在后退按钮上反应本机加载ios

时间:2019-05-03 06:23:31

标签: ios react-native webview spinner back-button

当我想返回ios设备时,webView应用程序出现问题,功能正确,但在加载webView时不显示微调框。我已经从webView创建了一个按钮,当我单击鼠标不起作用时,微调器(正在加载)。

如何显示此微调框?在android设备上不存在此问题,因为我控制了硬件按钮。

这是我的代码,谢谢!

import React from 'react';
import {ActivityIndicator, AppRegistry, StyleSheet, Text, View, WebView, BackHandler, Dimensions, Platform, StatusBar, topBar, script, TouchableOpacity} from 'react-native';
import { Button } from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';

export default class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      canGoBack: false,
      back: false
    }
  }

  showSpinner() {
    this.setState({loading:true});
  }
  hideSpinner() {
    this.setState({loading:false});
  }

  goBack = () => {
    this.refWeb.goBack();
    this.showSpinner();
  }

  render() {
    return (
    <View style={ this.state.loading === true ? styles.stylOld : styles.styleNew}>
        <View         onNavigationStateChange={this.onNavigationStateChange.bind(this)}>
        {(Platform.OS) === 'ios' && this.state.canGoBack && !this.state.loading ? (
                                    <TouchableOpacity onPress={this.goBack} style={{flexDirection: 'row', marginBottom:-20, marginTop:20, padding: 13}}>
                                    <Icon
                                        name="chevron-left"
                                        size={20}
                                        type="clear"
                                        color="#147EFB"
                                        style={{marginTop:3}}
                                    />
                                    <Text
                                       style={{
                                                color: "#147EFB",
                                                marginLeft: 5,
                                                fontSize:20
                                             }}>
                                       Back
                                    </Text>
                                    </TouchableOpacity>
                            ) : null
            }
            {this.state.loading ? (
                          <ActivityIndicator
                            color="#009688"
                            size="large"
                            style={styles.ActivityIndicatorStyle}
                          />
                        ) : null}
            </View>
    {this.state.loading ? (
              <ActivityIndicator
                color="#009688"
                size="large"
                style={styles.ActivityIndicatorStyle}
              />
            ) : null}
      <WebView
        style={styles.WebViewStyle}
        onLoadStart={() => this.showSpinner()}
        onLoad={() => this.hideSpinner()}
        ref={(myWeb) => this.refWeb = myWeb}
        onNavigationStateChange={this.onNavigationStateChange.bind(this)}
        renderLoading={this.ActivityIndicatorLoadingView}
        source={{uri: 'https://www.marca.com/'}}
        startInLoadingState
      />
      </View>
    );
  }


ActivityIndicatorLoadingView() {
   //making a view to show to while loading the webpage
   return (
     <ActivityIndicator
        color="#009688"
        size="large"
        style={styles.ActivityIndicatorStyle}
     />
   );
}

  componentDidMount() {
    if(Platform.OS != 'ios'){
      BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
      StatusBar.setHidden(true);
      }
  }

  componentWillUnmount() {
      if(Platform.OS != 'ios'){
      BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
      StatusBar.setHidden(false);
      }
  }

  onNavigationStateChange(navState) {
        this.setState({
        canGoBack: navState.canGoBack,
        loading: navState.loading
     });
   }

  handleBackPress = () => {
     if (this.state.canGoBack) {
        this.refWeb.goBack();
        return true;
      }
    else{
        return false;
      }
  }
}

const {width, height} = Dimensions.get('window');
const styles = StyleSheet.create({
   stylOld: {
        flex: 1,
        top: height/ 2,
        left: width / 2 - 20,
        position: 'absolute'
      },
   styleNew: {
        flex: 1,
      },
  WebViewStyle: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
    marginTop: (Platform.OS) === 'ios' ? 20 : 0
    },
  ActivityIndicatorStyle: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    position: 'absolute',
    marginTop: -40
    }
});

0 个答案:

没有答案