简体   繁体   English

React Native:WebView高度设置不正确

[英]React Native: WebView height isn't being set properly

I'm having a problem with a WebView in my React Native app. 我的React Native应用程序中的WebView出现问题。 When no height for the WebView is specified, it defaults to about half the screen height (iPhone 6S). 如果未指定WebView的高度,则默认为屏幕高度的一半(iPhone 6S)。 However, when I set a height with the help of Dimensions , it displays fine, but only the original half is interactive – ie. 但是,当我借助Dimensions设置高度时,它显示得很好,但是只有原始的一半是交互式的,即。 can only scroll using the top half of the screen. 只能使用屏幕的上半部分滚动。

Here are the main parts of my current code: 这是我当前代码的主要部分:

import React, { Component } from 'react';
import {
  ...
  Dimensions,
  ...
  WebView,
} from 'react-native';

let ScreenHeight = Dimensions.get("window").height;
let ScreenWidth = Dimensions.get("window").width;

class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        ...
        <WebView
          source={{uri: 'http://foo.com'}}
          style={{height: ScreenHeight, width: ScreenWidth}}
        />
        ...
      </View>
    );
  }
}

...

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#bbb',
    flex: 1,
  },
  ...
});

AppRegistry.registerComponent('myApp', () => App);

I look forward to any help that can be offered :) 我期待可以提供的任何帮助:)

Try adding flex style to WebView . 尝试将flex样式添加到WebView Your code will look like this: 您的代码将如下所示:

<View style={styles.container}>
    ...
    <WebView
        source={{uri: 'http://foo.com'}}
        style={{flex:1}}
    />
    ...
</View>

It's possible that an invisible view from your render function is being drawn on the bottom half of the screen. 您的渲染功能可能会在屏幕的下半部分绘制不可见的视图。 With out seeing the rest of the render method I couldn't tell, still worth checking. 由于看不到我无法分辨的其余渲染方法,仍然值得检查。

Thanks for the answers, guys. 谢谢大家的回答。 I've fixed my issue now, and it seems to be such an easy fix. 我已经解决了我的问题,这似乎很容易解决。 I basically wrapped the WebView in it's own View container. 我基本上将WebView包装在它自己的View容器中。 I'm not sure whether it wasn't working because it had sibling elements (ie. NavBar and TabBarIOS – which I left off my previous snippet – sorry!) , but the WebView now works great. 我不确定它是否起作用,因为它具有同级元素(即NavBarTabBarIOS –我从上一个片段中TabBarIOS了–对不起!) ,但是WebView现在可以很好地工作了。

Here is my new render function: 这是我的新渲染函数:

render() {
   return (
      <View style={styles.container}>
         <NavBar>
         <View>
            <WebView
               source={{uri: 'http://foo.com'}}
               style={{height: ScreenHeight, width: ScreenWidth}}
            />
         </View>
         <TabBarIOS>
            ...
         </TabBarIOS>
      </View>
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM