App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离

引入webview的页面添加safeAreaInsets ,对weview的webviewStyles做处理

在myApp中改造

entry.vue代码如下

javascript 复制代码
template>
	<view class="entry-page" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
		<web-view :webview-styles="webviewStyles" :src="webviewUrl" @message="getH5Message" ref="webViewRef"></web-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			statusBarHeight: 100,
			safeAreaInsets: {},
			webviewUrl: 'myLink', 
			hasBottomSafeArea: false,
			webviewStyles: {
				progress: {
					color: '#007aff',
					top: 0
				},
				// iOS侧滑返回配置
				ios: {
					allowsBackForwardNavigationGestures: true, // 启用WKWebView侧滑手势
					bounces: false // 禁用弹性效果
				}
			},
			webview: '',
			isIOS: false,          // 判断是否为iOS设备
			webviewCanBack: false  // WebView是否可返回
		};
	},
	onLoad(options) {
		// #ifdef APP-PLUS
		this.isIOS = uni.getSystemInfoSync().platform === 'ios'; // 检测iOS设备
		
		let _this = this;
		let height = 0;
		let statusbar = 0;
		const sysInfo = uni.getSystemInfoSync();
		this.safeAreaInsets = sysInfo.safeAreaInsets; //获取顶部安全距离
		console.log('top--------', sysInfo);
		this.statusBarHeight = sysInfo.statusBarHeight;
		height = sysInfo.windowHeight;
		let currentWebview = this.$scope.$getAppWebview();
		
		setTimeout(() => {
			var wv = currentWebview.children()[0];
			console.log('top--------222222222', _this.statusBarHeight);
			wv.setStyle({
				top: _this.statusBarHeight, //给weview设置顶部安全距离
				height: height - _this.statusBarHeight,
				scalable: false //禁止缩放
		}, 200);
		// #endif
	}
}
</script>

<style>
.entry-page {
	background-color: #f8f8f8;
}

.webview-container {
	flex: 1;
	width: 100%;
}
</style>
相关推荐
鹏北海3 小时前
Vue 3 超强二维码识别:多区域/多尺度扫描 + 高级图像处理
前端·javascript·vue.js
Android疑难杂症3 小时前
一文讲清鸿蒙网络开发
前端·javascript·harmonyos
爱学习的程序媛3 小时前
【JavaScript基础】Null类型详解
前端·javascript
前端一课3 小时前
uniapp之WebView容器原理详解
前端
CryptoRzz3 小时前
DeepSeek印度股票数据源 Java 对接文档
前端·后端
网络点点滴4 小时前
watch监视-ref基本类型数据
前端·javascript·vue.js
西洼工作室4 小时前
前端接口安全与性能优化实战
前端·vue.js·安全·axios
大布布将军4 小时前
《前端九阴真经》
前端·javascript·经验分享·程序人生·前端框架·1024程序员节
幸运小圣4 小时前
for...of vs for 循环全面对比【前端JS】
开发语言·前端·javascript
用户9545156811624 小时前
实际开发中 | 与 || 的使用方法及组件封装方案解析
前端