> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/screen/safe-area.md).

# Safe Area

在移动浏览器中，内容有时会因为状态栏或 Home 指示条等系统 UI 而被遮挡。Appintos SDK 提供了一个函数，用于以像素为单位计算屏幕安全区域（Safe Area）的边距值，以避免这种情况。

尤其是在 iPhone X 及以上设备或某些 Android 设备上，使用全屏的 Web 应用时，系统 UI 经常会遮挡内容。使用下面的函数，可以轻松调整边距，确保内容安全显示。

### `SafeAreaInsets`

当屏幕模式切换时 `safearea` 可以查看其值。切换屏幕时可以获取安全区域信息，也可以订阅变化。使用此功能，即使在各种设备环境中也能实现稳定的 UI。

* `SafeAreaInsets.get()` : 当前屏幕模式的 `safearea` 值。
* `SafeAreaInsets.subscribe()` : 每当屏幕模式改变时 `safearea` 订阅值变化。

{% hint style="info" %}
**游戏内 X 按钮坐标**

X 按钮已作为框架的默认组件提供。位于屏幕的 **右上角**并固定在那里，位置可按如下计算。

* **X 轴：** `safeAreaInsetsValue.right + 10`
* **Y 轴：** `safeAreaInsetsValue.top + 5` (iOS) / `safeAreaInsetsValue.top +10` (Android)

如果游戏内按钮与框架的 X 按钮重叠，可能会在审核过程中被驳回。
{% endhint %}

#### 示例

```tsx
import { SafeAreaInsets } from '@apps-in-toss/web-framework';
import { useEffect, useState } from 'react';

interface SafeAreaInsets {
  top: number;
  bottom: number;
  left: number;
  right: number;
}

function Page() {
  const [safeAreaInsetsValue, setSafeAreaInsetsValue] = useState<SafeAreaInsets>(() => SafeAreaInsets.get());
  // 导航栏顶部留白：safeAreaInsetsValue.top
  // 导航栏右侧留白：safeAreaInsetsValue.right + 10

  useEffect(() => {
    const cleanup = SafeAreaInsets.subscribe({
      onEvent: (insets) => {
        setSafeAreaInsetsValue(insets);
      },
    });
    return () => cleanup();
  }, []);

  // ...
}
```

### `getSafeAreaInsets`

{% hint style="info" %}
**此函数仅可用于 SDK 1.4.6 版本**

**从 1.4.7 版本起已废弃** 了
{% endhint %}

```ts
import { getSafeAreaInsets } from '@apps-in-toss/web-framework';

const insets = getSafeAreaInsets();
// 示例返回值：{ top: 44, bottom: 34 }
```

返回的对象包含以下属性。

* `top`: 为避免被顶部状态栏遮挡而需要预留的间距。
* `bottom`: 为避免被底部主页指示条遮挡而需要预留的间距。

此值在构建整个屏幕(`视口`）时经常使用。例如，可以为底部固定按钮留出间距，或者让顶部标题栏不要与状态栏重叠。

#### 底部留白应用示例

这是为避免底部固定按钮与主页指示条重叠而添加留白的示例。

```tsx
import { getSafeAreaInsets } from '@apps-in-toss/web-framework';

const insets = getSafeAreaInsets();

const Button = () => {
  return (
    <div style={{ paddingBottom: `${insets.bottom}px` }}>
      <button>确认</button>

  );
};
```

#### 顶部留白应用示例

这是为避免顶部固定页眉与状态栏重叠而添加留白的示例。

```tsx
import { getSafeAreaInsets } from '@apps-in-toss/web-framework';

const insets = getSafeAreaInsets();

const Header = () => {
  return (
    <div style={{ paddingTop: `${insets.top}px` }}>
      <h1>标题</h1>

  );
};
```

### `useSafeAreaInsets`

为了防止在移动浏览器中因状态栏或 Home 指示条等系统 UI 导致内容被遮挡，它会以像素为单位计算屏幕安全区域（Safe Area）的边距值。 `useSafeAreaInsets` 可在使用 React Native 开发时使用。

```tsx
import { useSafeAreaInsets } from '@granite-js/native/react-native-safe-area-context';
const { top: safeAreaTop, right: safeAreaRight } = useSafeAreaInsets();
// 导航栏上方边距：safeAreaTop
// 导航栏右侧边距：safeAreaRight + 10
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/screen/safe-area.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
