How to use SafeAreaInsets in SwiftUI | Bootcamp #65

Swiftful Thinking
9 Jun 202306:27

Summary

TLDR在这段视频中,Nick 介绍了在 Swift UI 中使用安全区域边距(safe area insets)的新方法。尽管这不是一个全新的概念,因为之前已经讨论过如何忽略或不忽略安全区域,但这个视频展示了如何在安全区域内有意识地放置视图、文本或按钮。Nick 通过创建一个名为 'safe area inset boot camp' 的新 Swift UI 文件,展示了如何利用安全区域边距来定位 UI 元素。他通过几个示例说明了如何在屏幕底部放置元素,包括使用对齐和间距属性来调整元素的位置。此外,Nick 还探讨了如何将元素放置在屏幕顶部,并且提到了如何创建一个固定在顶部的标题栏。虽然这个功能并不是非常常见,但视频提供了一个有用的工具,供开发者在需要时使用。

Takeaways

  • 📘 Swiftful Thinking 视频中,Nick 介绍了如何使用 iOS 16 中的新特性:安全区域边距(safe area insets)
  • 📌 安全区域边距不是替代之前关于安全区域的讨论,而是提供了一种新的方式来处理 UI 元素与安全区域的关系
  • 🔍 即使不常用,了解安全区域边距对于开发者来说也是一个有用的工具,可以在需要时使用
  • 📱 通过安全区域边距,开发者可以更容易地将视图、文本或按钮放置在屏幕的安全区域内
  • 🤔 Nick 认为使用安全区域边距有些棘手,并且不是特别喜欢这种方式,但仍然展示了如何使用
  • 🔄 通过几个快速示例,Nick 展示了如何利用安全区域边距在屏幕底部添加内容
  • 📊 可以利用安全区域边距将元素放置在屏幕的底部或顶部,这在不同的设备上可以更加动态
  • 📐 除了底部,还可以使用顶部安全区域边距,这在有导航标题的情况下看起来会稍有不同
  • 🔄 利用安全区域边距可以实现类似悬停按钮的效果,或者创建一个随着用户滚动而悬停的标题
  • 📊 可以创建一个固定在屏幕顶部的标题栏,即使在用户滚动时也能保持在顶部
  • 🛠️ 安全区域边距是 Swift UI 开发者的一个便利修饰符,它简化了之前需要手动管理安全区域的复杂性
  • ✅ 虽然不是所有应用都需要使用安全区域边距,但了解这个特性对于开发者来说是一个不错的选择

Q & A

  • Swiftful Thinking 的视频主要介绍了什么内容?

    -视频主要介绍了在Swift UI中如何使用安全区域边距(safe area insets)来放置视图、文本或按钮等UI元素。

  • 为什么说使用安全区域边距不是替代之前处理安全区域的方法?

    -因为安全区域边距是用于在安全区域内故意放置UI元素的一种新方式,而不是用来取代之前处理安全区域的方法,两者需要结合使用。

  • 在Swift UI中,如何使用安全区域边距来放置UI元素?

    -可以通过使用`safeAreaInset`修饰符,并指定边距的边缘(如`bottom`或`top`),然后添加对齐方式和内容来实现。

  • 视频提到了在iOS 16中有哪些关于安全区域边距的新特性?

    -视频提到了在iOS 16中,可以使用安全区域边距来放置UI元素,并且可以动态地适应不同设备的底部或顶部安全区域。

  • 为什么说使用安全区域边距放置UI元素比使用覆盖层(overlay)更简单?

    -使用覆盖层需要手动管理安全区域,而使用安全区域边距可以直接将元素放置在顶部或底部,不需要额外处理安全区域的逻辑。

  • 在Swift UI中,如何创建一个新的Swift UI视图文件?

    -可以通过在Navigator中右键点击并选择创建新文件,然后选择Swift UI View,并为文件命名。

  • 视频提到了哪些使用安全区域边距的可能场景?

    -视频提到了将文本或按钮放置在屏幕底部、创建悬浮按钮、固定标题栏等使用场景。

  • 为什么视频作者认为使用安全区域边距放置UI元素的方式有些棘手?

    -因为作者认为这种方式虽然提供了一种新的放置UI元素的方法,但在实际操作中可能不如预期的那样直观或容易,需要更多的尝试和调整。

  • 视频作者对于在屏幕底部放置UI元素有哪些建议?

    -作者建议可以使用安全区域边距来放置UI元素,并且可以设置不同的对齐方式,如居中、靠左或靠右,以及可以设置形状剪裁等。

  • 在Swift UI中,如何实现一个固定在屏幕顶部的标题栏?

    -可以使用`safeAreaInset`修饰符的`top`边缘,并结合`pinned()`修饰符来实现一个固定在屏幕顶部的标题栏。

  • 视频作者对于使用安全区域边距的总体态度是怎样的?

    -作者认为使用安全区域边距不是一个非常常见的做法,但他还是想展示给观众,以便他们在需要时可以使用这个工具。

Outlines

00:00

😀介绍安全区域插入

这段视频介绍了在Swift UI中使用安全区域插入的新修饰符。作者指出,虽然以前我们已经学习了如何管理安全区域以及如何忽略或不忽略安全区域,但这个新修饰符并不取代之前的方法,而是为了更灵活地在视图中放置特定的UI元素到安全区域内。虽然作者表示在应用程序中并不常用这个功能,但还是想让大家了解并掌握。作者创建了一个名为'safe area inset boot camp'的Swift UI视图,演示了如何使用这个新修饰符。通过示例演示了如何将UI元素放置在底部安全区域,并展示了一些可能的应用场景,如悬停按钮、固定标题等。最后,作者指出,虽然这个功能并不是以前做不到的事情,但它为Swift UI开发者提供了一种更方便的方式来管理安全区域内的UI元素。

05:01

😀使用安全区域插入的示例

这段视频展示了如何使用安全区域插入来在Swift UI中创建一些特定的UI布局。作者通过几个示例演示了如何在底部和顶部安全区域内放置UI元素,如悬停按钮、固定标题等。作者强调,虽然以前可以使用覆盖视图等方式实现类似效果,但使用安全区域插入更加方便,因为它不需要手动管理安全区域,而是直接将UI元素放置在指定的安全区域内。通过这些示例,观众可以了解到如何灵活地利用安全区域插入来创建不同的UI布局,增强应用程序的用户体验。

Mindmap

Keywords

💡Swiftful Thinking

Swiftful Thinking 是视频作者Nick的频道名称,专门讨论与Swift编程语言和Swift UI框架相关的内容。在视频中,Nick通过Swiftful Thinking向观众展示如何在Swift UI中使用安全区域边距(safe area insets)这一新特性。

💡安全区域边距(safe area insets)

安全区域边距是Swift UI中的一个概念,指的是在iOS设备上,屏幕边缘(如刘海屏或圆角)周围的可触摸区域。在视频中,Nick讲解了如何使用这个特性来放置视图或控件,确保它们不会超出屏幕的安全显示区域。

💡Swift UI

Swift UI是一个用于构建用户界面的框架,允许开发者以声明式的方式创建iOS、macOS、tvOS和watchOS应用的界面。视频中,Nick展示了如何利用Swift UI的新特性safe area insets来优化用户界面设计。

💡iOS 16

iOS 16是苹果公司的移动操作系统的一个版本,视频中提到的safe area insets是iOS 16中的一个更新特性。Nick在视频中提到,虽然他不经常使用这个特性,但它为开发者提供了新的UI设计选项。

💡导航栈(navigation stack)

导航栈是Swift UI中的一个组件,用于管理应用中的导航结构,允许用户在不同的视图之间进行切换。在视频中,Nick创建了一个导航栈,并在其中添加了一个列表视图,以展示如何使用safe area insets。

💡矩形(rectangles)

在Swift UI中,矩形是一个基本的UI组件,用于在屏幕上绘制矩形形状。Nick在视频中使用矩形来构建一个基本的UI界面,并通过设置高度和背景颜色来展示如何将它们放置在屏幕上。

💡导航标题(nav title)

导航标题是Swift UI中用于显示当前导航视图标题的组件。在视频中,Nick添加了一个导航标题,并命名为“safe area insets”,以反映视频的主题和教学内容。

💡覆盖层(overlay)

覆盖层是Swift UI中的一个概念,允许在现有视图之上添加额外的视图。Nick在视频中提到,虽然可以通过覆盖层来实现将元素放置在安全区域内,但使用safe area insets提供了一种更简单的方法。

💡对齐(alignment)

在Swift UI中,对齐是指如何将UI元素在它们的父容器中定位。Nick在视频中展示了如何使用对齐属性来控制放置在安全区域边距内的元素的位置,例如将其对齐到中心、领先边缘或尾部边缘。

💡间距(spacing)

间距在Swift UI中用于指定元素之间的空白区域大小。虽然在视频中Nick提到,由于只放置了一个项目,间距并不重要,但它是一个重要的布局属性,可以用来调整UI元素之间的视觉间隔。

💡内容(content)

在Swift UI中,内容是指视图或视图修饰符中包含的UI元素。Nick在视频中通过将一个高亮的矩形(High)作为内容放入safe area insets修饰符中,展示了如何将特定的UI元素放置在屏幕的安全区域内。

Highlights

Nick介绍了如何在Swift UI中使用安全区域边距(safe area insets)来放置视图、文本或按钮。

安全区域边距是iOS 16中的一个新特性,允许开发者在屏幕的安全区域内放置UI元素。

即使在之前已经讨论过安全区域,安全区域边距并不是替代品,而是提供另一种处理方式。

通过使用安全区域边距,可以更简单地将元素放置在屏幕底部等安全区域内。

Nick展示了如何创建一个新的Swift UI视图文件,并命名为'safe area inset boot camp'。

安全区域边距可以用于动态地适应不同设备的安全区域。

通过使用safe area inset,可以避免手动管理安全区域的复杂性。

Nick通过示例演示了如何在屏幕底部添加一个黄色背景的文本,并使其位于安全区域内。

除了底部,safe area insets还可以用来放置顶部的元素,如导航标题下方。

safe area insets提供了一种新的方式来创建悬浮按钮或固定标题,而无需复杂的布局代码。

Nick展示了如何将一个黄色条固定在屏幕顶部,即使在滚动时也能保持可见。

safe area insets提供了一种简便的方法来处理UI元素与屏幕边缘的关系,而无需手动计算。

Nick提到,尽管safe area insets非常有用,但在实际应用中并不常见。

通过safe area insets,开发者可以更容易地实现在屏幕边缘的UI设计,如悬浮按钮或固定标题。

Nick强调了safe area insets作为Swift UI开发者工具箱中的另一个便利修饰符的价值。

即使开发者可能不会经常使用safe area insets,了解其功能和用法对于处理特定UI布局非常有用。

Nick在视频结束时感谢观众,并预告了下一个视频的内容。

Transcripts

play00:00

all right welcome back everybody my name

play00:02

is Nick this is swiftful thinking and

play00:04

this is gonna be a quick video on the

play00:05

safe area insets

play00:07

so previously I've covered the safe area

play00:10

and how we can ignore or not ignore the

play00:12

safe area this is kind of along the same

play00:14

lines but it is not replacing that we

play00:17

still need to do that in our code this

play00:19

is more for if we have a view or a text

play00:21

or a button that we purposely want to

play00:24

put in the safe area we can use this new

play00:27

modifier I don't think this is super

play00:29

common I haven't really used it much in

play00:31

apps but I wanted to show to you guys

play00:32

get it in your toolbox so at least you

play00:34

know it's there in case you want to use

play00:37

it

play00:44

[Music]

play00:46

all right a relatively quick video here

play00:49

just something I want to show you guys I

play00:50

don't use it that often but it is kind

play00:53

of cool nice little update in iOS 16.

play00:55

let's right click the Navigator create a

play00:57

new file this will be a swift UI View

play01:00

and I'm going to call this safe area

play01:02

inset boot camp

play01:05

click create now if you did follow this

play01:07

bootcamp of course you know that I did a

play01:09

whole entire video on how to use the

play01:11

safe area in Swift UI that is still

play01:14

applicable this is not replacing that we

play01:16

still need to manage the safe area in

play01:18

that same exact way this however allows

play01:21

us to put a little bit of code some UI

play01:24

inside that safe area if we want to

play01:28

so it's a little tricky to work with and

play01:31

I'm honestly not a fan of how tricky it

play01:33

is but let's take a look at a couple

play01:35

quick examples here so I'm going to do a

play01:38

navigation stack open the brackets let's

play01:41

put in a maybe a list here and I'm just

play01:45

going to go from zero to less than 10.

play01:48

and let's just put some rectangles on

play01:49

the screen let's give it a height of

play01:52

maybe

play01:53

I don't know what's a good height guys

play01:55

300 sure

play01:57

and there we go all right so you have

play01:59

this nice basic UI that we can scroll

play02:01

let's also add in a nav Title Here of

play02:04

maybe let's call a safe area insets

play02:09

and the the problem in question is

play02:12

basically like how do we put something

play02:13

in the safe area at the bottom here

play02:16

now there are existing ways to do this

play02:18

right I could do like an overlay

play02:22

and maybe I put in like a text here that

play02:25

says hi

play02:27

let's give it a background

play02:30

color. yellow

play02:33

and maybe I align that overlay

play02:36

to the bottom

play02:37

right so now I am at the bottom let's do

play02:40

a Max frame

play02:42

premium Max width Infinity I do have it

play02:46

in the safe area and it's over the

play02:48

bottom so that actually does work right

play02:49

so this is not really solving a problem

play02:51

that was unsolvable before but a new

play02:54

easy way to do this is to call the safe

play02:57

area inset

play03:00

and we can give it an edge the edge of

play03:02

course is going to be the bottom

play03:04

the alignment let's just keep it as

play03:05

Center for a second

play03:07

spacing doesn't matter because we're

play03:09

going to put one item for now and

play03:11

content so I'm going to take this High

play03:13

I'm just going to put it in here

play03:16

uncommented out

play03:18

and now we have the exact same UI I

play03:20

guess this is maybe going to be a little

play03:22

more Dynamic on different devices but

play03:25

that is just the newer way to do it

play03:27

basically

play03:29

I I have not used this very often in

play03:31

apps because it's not super common but

play03:35

let's just play around with it a little

play03:36

bit before we end this video

play03:38

so maybe I get rid of the max frame here

play03:40

we can see that it's aligned to the

play03:41

center I could also align it to maybe

play03:43

the Leading Edge so now it's over here

play03:46

so this could be like if you want to do

play03:48

like a hovering button here maybe you do

play03:50

like some padding

play03:53

and and maybe there's some padding

play03:57

before as well just so it's a little bit

play03:58

bigger and maybe you clip shape it into

play04:02

a circle

play04:05

now you have like a hover button on the

play04:07

bottom left corner we could also do

play04:08

trailing so it's in the bottom right

play04:10

corner

play04:11

one thing that is unique to the safe

play04:13

area insets is that we can do the top

play04:15

so the Top's gonna look a little

play04:16

different with the navigation title here

play04:18

so it's actually like right below the

play04:21

navigation title

play04:23

so this could be cool if you wanted to

play04:25

do some sort of like hover as the user

play04:27

Scrolls

play04:28

I think at the top maybe a little more

play04:30

common use case would be like a pinned

play04:32

header so if you use the lazy V Stacks

play04:35

you have the option to pin the headers

play04:36

but it's kind of like automatically pins

play04:39

so we could do something like this where

play04:42

we maybe

play04:45

let's see add in just like a yellow bar

play04:47

at the top so when the user Scrolls now

play04:50

we have that high just pinning to the

play04:51

top right something that's maybe a

play04:54

little bit unique

play04:55

we can call it maybe not edgesignoring

play04:57

safe area dot bottom so it's not

play04:59

ignoring the top

play05:00

and then you know we have just this nice

play05:02

pinned header here

play05:04

it looks a little funky when this

play05:06

Scrolls down I'm not a huge fan of that

play05:08

UI but now you know at least that you

play05:10

can do this maybe you put some filters

play05:13

here maybe it's just a some sort of

play05:15

hovering title that's next to the safe

play05:18

area of course if I change the nav title

play05:21

here maybe I wanted to do like a nav bar

play05:25

display in line now I have that like pin

play05:29

header just like always at the top which

play05:31

is kind of cool

play05:33

yeah so again that's basically all we're

play05:35

covering in this video this is not

play05:37

anything that couldn't be done without

play05:39

the safe area insets like you could draw

play05:41

overlays you could draw backgrounds but

play05:44

when you're doing those overlays you

play05:45

have to kind of manage the safe area

play05:47

yourself you have to understand whether

play05:49

or not you're in the safe area or not

play05:51

whether you're ignoring it or not and

play05:55

here we can just

play05:56

throw it into the top or the bottom and

play05:59

call it a day which is yet another

play06:01

convenience modifier for Swift UI

play06:04

Developers

play06:05

all right maybe you're not going to use

play06:07

this but at least now you know how to

play06:09

use it so thank you guys for watching as

play06:11

always I'm Nick this is swifflethinking

play06:14

and I'll see you in the next video

play06:18

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Swift UI安全区域iOS 16界面设计编程教程Swiftful ThinkingNick动态UI导航栏视频教程Swift开发
Do you need a summary in English?