react-native-webview禁止缩放

Posted bbcfive

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react-native-webview禁止缩放相关的知识,希望对你有一定的参考价值。

一、需求

RN-webview禁止缩放,即固定屏幕大小,但同时要能够监听到其他手势识别

二、实现

仅仅设置webview的大小自适应是不够的,因为webview所引入的h5页面有可能是能够缩放的。

因此先向h5页面注入代码:

const INJECTEDjavascript = `
  const meta = document.createElement(‘meta‘); 
  meta.setAttribute(‘content‘, ‘initial-scale=0.5, maximum-scale=0.5, user-scalable=0‘); 
  meta.setAttribute(‘name‘, ‘viewport‘); 
  document.getElementsByTagName(‘head‘)[0].appendChild(meta); 
`

而后设置webview:

<WebView
  ref=ref => (this.webview = ref)
  javaScriptEnabled=true
  scalesPageToFit=false
  injectedJavaScript= INJECTEDJAVASCRIPT 
  source= uri: this.state.source 
/>

即可固定页面。

以上是关于react-native-webview禁止缩放的主要内容,如果未能解决你的问题,请参考以下文章

移动端Vue禁止页面被缩放和放大

想请问,iOS10网页viewport怎么禁止缩放

手机页面禁止缩放代码

ios10中禁止用户缩放页面

完美解决ios10及以上Safari无法禁止缩放的问题

ios 双击页面缩放 禁止