You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取iOS设备底部Home导航条的高度?

获取系统导航栏高度以避免底部重叠的方案

根据不同平台和技术栈,这里提供几种可靠的实现方式:

Android 原生实现

1. 直接获取导航栏资源高度(API 21+)

通过系统资源ID获取导航栏高度,适用于需要手动设置padding的场景:

val resources = context.resources
val navBarHeightResId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (navBarHeightResId > 0) {
    val navBarHeight = resources.getDimensionPixelSize(navBarHeightResId)
    // 为底部布局设置paddingBottom
    bottomLayout.setPadding(0, 0, 0, navBarHeight)
}

2. 利用WindowInsets适配(API 30+,推荐)

通过监听WindowInsets可以自动获取当前可见的导航栏高度(兼容导航栏隐藏/显示的场景):

view.setOnApplyWindowInsetsListener { _, insets ->
    val navBarHeight = insets.systemWindowInsetBottom
    view.setPadding(0, 0, 0, navBarHeight)
    insets
}

iOS 原生实现

iOS的底部安全区域已经包含了Home Indicator(底部导航条)的高度,直接使用安全区域约束即可:

1. 代码设置padding

guard let window = UIApplication.shared.windows.first else { return }
let bottomSafeInset = window.safeAreaInsets.bottom
bottomView.frame.origin.y = window.frame.height - bottomView.frame.height - bottomSafeInset

2. AutoLayout约束(推荐)

直接将底部视图约束到安全区域底部,无需手动计算高度:

bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

跨平台框架实现

React Native

推荐使用react-native-safe-area-context库获取安全区域Insets:

import { useSafeAreaInsets } from 'react-native-safe-area-context';

const App = () => {
  const insets = useSafeAreaInsets();
  return (
    <View style={{ paddingBottom: insets.bottom }}>
      {/* 底部内容 */}
    </View>
  );
};

也可以直接使用内置的SafeAreaView组件自动适配:

import { SafeAreaView } from 'react-native';

<SafeAreaView style={{ flex: 1 }}>
  {/* 页面内容 */}
</SafeAreaView>

Flutter

通过MediaQuery获取底部安全区域padding:

@override
Widget build(BuildContext context) {
  final bottomPadding = MediaQuery.of(context).padding.bottom;
  return Scaffold(
    body: Padding(
      padding: EdgeInsets.only(bottom: bottomPadding),
      child: /* 页面内容 */,
    ),
  );
}

或使用SafeArea组件自动适配:

SafeArea(
  child: Scaffold(
    body: /* 页面内容 */,
  ),
)

注意:优先使用系统或框架提供的安全区域适配方案,而非手动固定高度,这样能更好兼容不同设备(如折叠屏、全面屏)的差异。

内容的提问来源于stack exchange,提问作者Mahdi Dahouei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 12:14:58