SwiftUI|safeAreaInset 修饰器

了解如何使用 SwiftUI 中的 safeAreaInset 修饰器。

SwiftUI|safeAreaInset 修饰器

.safeAreaInset() 是在 iOS 15 / macOS 12 (Monterey) 中引入的 SwiftUI 修饰器。

用于在安全区域边缘添加固定内容,同时保证内容不会被系统 UI 元素(如刘海、动态岛、Home 指示器等)遮挡。

在顶部固定内容

添加 Segmented Control 页面切换

如果你想在导航栏中添加视图,可以使用 safeAreaInset(.top),并通过 toolbarBackground 可见性修饰符,实现移除分隔线并合并背景。

safeAreaInset(.top)
未设置 toolbarBackground
通过 toolbarBackground 隐藏背景分割线
https://x.com/Dimillian/status/1838859581444796504

下面这个也类似:

https://x.com/jsedlacekjr/status/1715893840714858770/video/1

在底部固定内容

在 Paywall 中固定商品

在 Paywall 页面底部固定商品列表

https://x.com/onmyway133/status/1727329308199256183

添加文本输入框

https://x.com/natpanferova/status/1590676836488732678

添加固定按钮

https://x.com/CameronBardell/status/1403880009048145920/video/1

或者也可以添加类似相机的拍照按钮。