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>
相关推荐
刘发财2 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶5 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶5 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol7 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路9 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide9 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter9 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸10 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live0000011 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉11 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化