How to use Popover modifier in SwiftUI | Bootcamp #69

Swiftful Thinking
7 Nov 202318:03

Summary

TLDR在这段视频中,Nick介绍了Swift UI中的一个新特性——弹出框(Popover)。他首先提到了之前发布的一些高级视频,然后决定转换话题,探讨一些相对简单的Swift UI特性,特别是iOS 17中的新特性或之前未覆盖的内容。Nick通过创建一个简单的屏幕,使用ZStack和Button,展示了如何使用Popover修饰符。他解释了Popover如何基于当前屏幕上的内容显示,并提供了一个交互式的示例,展示了如何通过点击按钮触发Popover的弹出。接着,他讨论了Presentation Compact Adaptation修饰符,这是iOS 16.4及以上版本中的一个新特性,它允许开发者根据不同的屏幕尺寸自定义Popover的显示方式。Nick还演示了如何通过调整Popover的附着点(Attachment Anchor)来改变其在屏幕上的位置,同时强调了Apple在确保Popover在屏幕上正确显示方面所做的努力。最后,他通过一个实际的例子,展示了如何在应用程序中使用Popover来收集用户的反馈选项。整个视频以简洁明了的方式介绍了Popover的使用,展示了其在提供用户界面交互时的便利性和强大功能。

Takeaways

  • 🚀 Swift UI 17引入了新的modifier——popover,它允许在屏幕上当前内容的上方显示内容,非常适合用于提示信息框或弹出菜单。
  • 📚 Nick提到,尽管之前已经讨论过sheets,但这次将专注于使用Swift UI原生的popover。
  • 🔄 popover的使用方式与sheets或full screen cover类似,但需要添加一个新的modifier——presentation compact adaptation。
  • 📱 在iOS 16.4或更新版本中,presentation compact adaptation才可用,因此需要确保项目设置为支持iOS 16.4。
  • 📍 popover的内容会基于放置modifier的视图出现,这意味着popover会显示在它被放置的视图的正上方。
  • 🔧 可以通过popover的attachment anchor来自定义popover出现的位置,但popover的实际显示位置由Apple的逻辑决定,以确保其在屏幕上可见。
  • 🎨 使用popover时,可以自定义其arrow edge,但在iOS上通常不需要设置,因为arrow edge主要用在Mac OS上。
  • 📘 popover提供了一个内置的dismissal UX,用户可以通过点击屏幕上的任何地方来关闭popover。
  • 🔖 popover的内容非常简单,可以快速绑定布尔值来控制其显示和隐藏。
  • 📊 Nick通过一个实际例子展示了如何使用popover来创建一个反馈选项的弹出窗口,其中包含了不同的emoji表情符号。
  • 📈 popover的使用简化了在Swift UI中实现弹出内容的复杂性,提供了一种更简洁、更用户友好的交互方式。
  • 🔗 如果需要更多的自定义选项,可以预见未来Swift UI可能会更多地使用popover,而不是sheets或full screen cover。

Q & A

  • Swift UI中的Popover是一个什么样的组件?

    -Popover是Swift UI中用于在屏幕上显示内容的一个组件,它可以显示在当前内容的上方,类似于一个弹出窗口,非常适合用于提示信息框、菜单等。

  • 在Swift UI中,如何创建一个Popover?

    -在Swift UI中,创建Popover需要使用`presentation`修饰符,并绑定到一个布尔值来控制其显示与隐藏。内容部分可以是任何Swift UI视图。

  • 如何改变Popover在屏幕上的位置?

    -改变Popover的位置可以通过设置`attachment anchor`参数来实现,它可以指定Popover相对于触发它的视图的位置,如顶部、底部、中心等。

  • Popover和Sheet在Swift UI中有什么区别?

    -Popover和Sheet在Swift UI中都是用于模态展示的组件,但Popover更倾向于显示在触发它的特定视图上方,而Sheet则是从屏幕底部弹出的内容。

  • 在iOS 16.4之前的版本中,可以使用Popover特性吗?

    -Popover特性需要iOS 16.4或更新的版本支持。如果项目设置的最低iOS版本低于16.4,将无法使用Popover,需要更新Xcode到支持的版本。

  • 如何让Popover在用户点击屏幕的任何地方时都能被关闭?

    -Popover默认具有点击外部区域关闭的行为,这是Apple提供的一个内置功能,无需开发者额外编写代码。

  • Swift UI中的`presentation compact adaptation`修饰符是做什么用的?

    -`presentation compact adaptation`修饰符用于自定义Popover在不同尺寸设备上的展示方式,可以指定Popover在水平或垂直紧凑尺寸类别下的展示效果。

  • 在Swift UI中,Popover的箭头方向(Arrow Edge)如何影响其展示?

    -在iOS上,`Arrow Edge`并不影响Popover的展示,因为箭头方向主要用于MacOS系统。在iOS上,Popover的方向和位置完全由`attachment anchor`决定。

  • 如何让Popover在屏幕上的特定视图上显示?

    -将Popover修饰符直接添加到需要触发Popover的视图上,Popover将会出现在该视图的上方或指定的`attachment anchor`位置。

  • 在Swift UI中,Popover的使用场景有哪些?

    -Popover适用于显示提示信息、选项菜单、快速操作等场景,特别是当需要在不离开当前视图的情况下向用户提供信息或选项时。

  • 如何在Swift UI中实现一个带有多个选项的Popover?

    -可以使用`ForEach`循环来遍历选项数组,并为每个选项创建一个按钮,然后将这些按钮放置在Popover的内容视图中。如果选项较多,还可以将内容包裹在`ScrollView`中。

  • Swift UI中的Popover是否支持在横屏和竖屏模式下有不同的展示效果?

    -是的,通过`presentation compact adaptation`修饰符,可以为横屏和竖屏模式设置不同的展示效果,以适应不同方向下的屏幕尺寸。

Outlines

00:00

😀 Swift UI 高级学习回顾与新内容预告

Nick 欢迎观众回到 Swift UI 高级学习频道,提到之前发布的视频内容非常高级和复杂。他认为现在是时候转换一下,从困难的内容中休息一下,开始探讨一些尚未在频道中介绍的更简单的主题。接下来的视频将涉及 iOS 17 中的新内容或者之前未覆盖的内容。Nick 强调这些内容并非 Swift UI 应用开发的核心基础,因此他计划快速通过这些主题,视频时长控制在 10 到 15 分钟左右。首个视频将讨论一个他很少使用的新的修饰符——popover,它用于在屏幕上显示当前内容之上的某些信息。

05:02

📱 Popover 修饰符的介绍和使用

Nick 介绍了 popover 修饰符,这是一个在屏幕上显示内容的新的 Swift UI 特性。他解释了如何使用这个修饰符,并指出它与 sheets 和 full screen cover 类似,但具有不同的呈现方式。通过使用 presentation compact adaptation 修饰符,可以控制 popover 的显示方式,以适应不同的设备尺寸。他还提到,从技术上讲,sheet 和 full screen cover 可以视为 popover 的一种形式,并通过改变一个小的枚举值来自定义显示方式。Nick 通过一个实际的例子展示了如何使用 popover,包括如何自定义其出现位置以及如何利用系统提供的逻辑确保 popover 在屏幕上的正确位置显示。

10:02

🔍 Popover 的自定义和屏幕适配

Nick 继续探讨 popover 的自定义选项,特别是 attachment anchor 和 arrow edge 的使用。他指出,在 iOS 上 arrow edge 不会产生任何效果,因此主要关注 attachment anchor 的自定义。通过改变 attachment anchor 的位置,可以控制 popover 相对于触发它的按钮的出现位置。他还提到了苹果公司如何通过智能逻辑确保 popover 在屏幕上的正确显示,即使在屏幕边缘的按钮上触发 popover,它也会在屏幕上可见的区域内显示。

15:04

💡 实际应用中 popover 的使用示例

Nick 通过一个实际的应用示例,展示了如何使用 popover 来创建一个简单的用户反馈界面。他创建了一个包含不同反馈选项的数组,并在 popover 中为每个选项设置了一个按钮。通过这种方式,用户可以方便地选择他们的反馈,而不需要离开当前屏幕或进入一个新的屏幕。他还提到,如果选项较多,可以将这些按钮放入一个滚动视图中,以便用户可以滚动查看所有选项。最后,Nick 强调了 popover 的易用性和强大功能,并鼓励开发者在应用中使用它来提供更好的用户体验。

Mindmap

Keywords

💡Swift UI

Swift UI 是苹果公司开发的一种用于创建用户界面的框架,它允许开发者使用Swift语言以声明式的方式构建iOS、iPadOS、macOS、tvOS和watchOS应用的界面。在视频中,Swift UI 作为主要的开发环境,被用来展示如何实现和使用新的界面元素,如 popover。

💡Popover

Popover 是一种用户界面元素,用于在屏幕上显示临时的、浮动的窗口,通常用来显示菜单、提示信息或其他内容。在视频中,popover 被用作一种新的modifier,允许开发者在Swift UI中以一种简单且灵活的方式展示内容。

💡Presentation Adaptation

Presentation Adaptation 是Swift UI中的一个功能,它允许开发者根据不同的设备尺寸和方向来调整弹出内容的展示方式。在视频中,通过使用presentation compact adaptation,可以使得popover在不同设备上有更好的适应性和用户体验。

💡Attachment Anchor

Attachment Anchor 是popover的一个属性,它定义了popover相对于其附着点的位置。在视频中,通过调整attachment anchor,可以控制popover相对于触发它的按钮或视图的位置,从而实现不同的展示效果。

💡Sheet

Sheet 是Swift UI中的另一种弹出式界面元素,通常用于展示模态视图。在视频中,sheet与popover进行了比较,说明了它们在展示方式上的区别,以及如何通过presentation compact adaptation来实现popover的效果。

💡Modifiers

Modifiers 是Swift UI中用于修改视图行为和外观的工具。在视频中,modifiers被用来实现popover的展示,包括如何绑定状态、如何设置popover的呈现方式等。

💡State

State 在Swift UI中指的是状态,它允许视图持有并管理数据。在视频中,使用@State private var来定义了一个控制popover显示与隐藏的布尔值状态。

💡Feedback Options

在视频中,feedback options 是一个示例,用来展示如何使用popover来创建一个用户反馈界面。这个界面允许用户通过点击不同的emoji按钮来选择他们的反馈选项。

💡Customization

Customization 在视频中指的是对用户界面元素进行个性化设置的能力。对于popover,可以通过调整attachment anchor和使用不同的modifiers来定制其外观和行为,以适应不同的使用场景。

💡UX (User Experience)

UX 指的是用户体验,它是衡量产品或服务如何满足用户需求和期望的指标。在视频中,通过使用popover和其dismissal特性(点击屏幕任意位置可关闭popover),提供了一个流畅且直观的用户体验。

💡Dismissal

Dismissal 是指用户界面元素的关闭或消失行为。在视频中,popover的dismissal特性被提及,用户可以通过点击popover外部的屏幕区域来关闭它,这是一种提升用户体验的设计。

Highlights

Nick介绍了Swift UI的新特性,即弹出窗口(Popover)

Popover用于在屏幕上显示内容,可以覆盖当前内容

Popover适用于展示提示信息框、菜单等

Nick计划在接下来的视频中介绍iOS 17中的新特性或尚未涉及的内容

Swift UI的Popover功能允许开发者快速简单地实现弹出窗口效果

Popover与Sheet和FullScreenCover类似,但提供了更灵活的展示方式

使用`presentation: .compactAdaptation()`可以调整Popover的展示方式

Popover的锚点(attachment anchor)可以自定义,以适应不同位置的展示需求

Apple为Popover提供了智能的屏幕适配逻辑,确保内容始终在屏幕上可见

Nick通过一个简单的示例展示了如何使用Popover创建一个反馈选项菜单

Popover提供了一种比传统的Sheet更简洁、更快速的交互方式

通过Popover,开发者可以轻松实现在不同设备上的良好用户体验

Nick预测Popover将成为Swift UI中越来越常用的特性

Popover的实现简单,与Swift UI的其他功能保持一致,易于开发者上手

Nick鼓励观众订阅频道,以便不错过后续的Swift UI教程

视频最后,Nick总结了Popover的优势,并鼓励在适当的情况下使用它

Transcripts

play00:00

welcome back everybody my name is Nick

play00:02

this is swiftel thinking and if you are

play00:04

up to date on this channel you know that

play00:06

the last bunch of videos that I posted

play00:08

were in the Swift UI Advanced learning

play00:11

playlist they were really Advanced

play00:13

complex topics some of those videos got

play00:16

really long and really difficult so I

play00:18

think it's a good time now to maybe

play00:19

switch gears and maybe take a break from

play00:21

the hard stuff and look at some of the

play00:24

easier things that I have not yet

play00:25

covered on this channel so the next

play00:27

couple videos are going to be things

play00:28

that are either new to iOS and iOS 17 or

play00:32

things that I just haven't covered yet a

play00:34

lot of these things are not really core

play00:36

and foundational to building Swift UI

play00:38

apps so I'm going to try to move a

play00:39

little quickly here and keep these

play00:41

videos shorter maybe like 10 to 15

play00:43

minute Mark something in there this

play00:45

first video we're going to discuss a new

play00:47

modifier that I have very rarely used on

play00:49

this channel which is the popover this

play00:52

is a great way to literally show

play00:54

something on top of the current content

play00:55

on the screen and literally have it pop

play00:57

over your current content so this this

play01:00

is great for maybe things like little

play01:01

tips and info boxes maybe some popover

play01:04

menus and things like that we're going

play01:06

to look at all the different ways that

play01:07

we can use the popover modifier and then

play01:09

we're going to discuss a couple

play01:10

different

play01:20

implementations welcome back everybody

play01:23

we are back here again in the Swift UI

play01:25

boot camp playlist this is the original

play01:27

playlist on this channel and I'm just

play01:29

going to add a couple more videos here

play01:31

again because Swift UI just keeps

play01:33

getting better and we keep getting more

play01:34

and more really cool features uh this

play01:37

feature included I wish we had in the

play01:39

original version of Swift UI but I am

play01:41

thankful and happy that we have it at

play01:44

least now so what we're going to cover

play01:47

is called a popover and before we begin

play01:49

I just want to throw out there that

play01:50

earlier in this series we did a what I

play01:53

called a popover boot camp and in that

play01:56

video we used sheets and made our own

play01:59

custom popovers but going forward if we

play02:02

refer to something as a popover I guess

play02:04

we should use the native Swift UI

play02:06

version of the popover so you can call

play02:09

these modal transitions or sheets that

play02:11

we discussed earlier what we're going to

play02:13

do in this video is what I will actually

play02:15

call a popover going forward so let's

play02:17

right click the Navigator create a new

play02:19

file it'll be a swift UI View and let's

play02:21

call this native

play02:23

popover boot

play02:25

camp let's click create let's jump

play02:28

inside and I'm just going to close the

play02:30

navigator quick and make the text a

play02:32

little bigger so that you guys can see

play02:34

it and this should be a really easy one

play02:36

because a lot of the code is very very

play02:38

similar to a lot of the other Swift UI

play02:40

modifiers that we've already learned so

play02:43

let's start this one with a super simple

play02:45

screen I'm going to make a zstack here

play02:47

and on the background let's do color.

play02:49

gray. ignore safe area and then above

play02:52

that I'm just going to put a quick

play02:53

button the button will say something

play02:56

simple and let's click enter on the

play02:57

action and the modifier we're going to

play02:59

look at in this video is actually called

play03:02

hop over there are two completions here

play03:05

this one is binding to an identifiable

play03:07

and this one is binding to and is

play03:09

presented this works basically the same

play03:11

way as sheet or full screen cover where

play03:13

we we can bind to an item or bind to is

play03:16

presented same thing as full screen

play03:18

cover right we're just going to use

play03:20

popover now so the the code should be

play03:23

very very familiar to you guys we're

play03:25

going to use the is presented because

play03:26

it's a little easier to use for this

play03:28

tutorial we're going to buy into a

play03:30

Boolean so let's create an at

play03:33

State private VAR let's call it show

play03:37

popover of type bu set it equal to false

play03:41

and then of course we need to bind to it

play03:43

with the dollar sign show popover let's

play03:45

click enter on the content and we're

play03:47

going to keep the content super simple

play03:49

for now let's just put a quick text here

play03:52

that says maybe hello

play03:54

subscribers and this will be a quick

play03:56

friendly reminder that if you are

play03:58

watching this playlist list and you're

play04:00

not subscribed please hit that subscribe

play04:02

button I think a large portion of you

play04:04

are not subscribing but watching the

play04:06

videos and as amazing that is really

play04:08

does help me out if you could just hit

play04:09

that button but whether or not you're

play04:12

going to click that subscribe button

play04:13

you're definitely going to click this

play04:15

button here so in this button we should

play04:17

put a little action that says show pop

play04:19

over and maybe do toggle all right if I

play04:22

click this let's see what happens and

play04:24

boom we have a sheet pop up you're

play04:27

probably wondering hey Nick we said

play04:29

we're going to look at popovers but this

play04:30

looks like a sheet and we've already

play04:32

covered sheets in this playlist and

play04:34

you're absolutely correct and I was

play04:36

super confused about this the first time

play04:38

I did it as well why does a popover look

play04:40

like a

play04:41

sheet well it turns out there's another

play04:43

modifier that we need to use here and

play04:46

it's called

play04:47

presentation compact

play04:51

adaptation and let's type in here and

play04:54

there's two completions here this one

play04:56

allows you to customize the presentation

play04:58

for horizontally or vertically compact

play05:01

Siz classes so if you're using like a

play05:04

larger or smaller device that's

play05:05

horizontal or vertical you can customize

play05:08

the adaptation but in this video we're

play05:10

just going to use a simple compact

play05:12

adaptation just for a universal

play05:14

adaptation for all devices I think it's

play05:17

probably much more common um unless

play05:18

you're really customizing your app for

play05:21

different device sizes if I click the

play05:24

period here we're going to see there's a

play05:26

couple completions and it turns out that

play05:28

sheet is one of the adaptations that we

play05:31

can use same with full screen cover and

play05:34

then of course we have our new one which

play05:36

is popover so let's click do sheet here

play05:39

just because we already saw what the

play05:40

sheet looks like when I do that we're

play05:43

going to get a quick error message of

play05:44

course presentation compact adaptation

play05:46

is only available in iOS 16.4 or newer

play05:50

I'm not going to deal with versioning in

play05:51

this video so let's just go ahead and

play05:53

change our project go to the Navigator

play05:56

and make our project compile for iOS 16

play05:59

16.4 if you don't have 16.4 here you're

play06:03

probably using an older version of xcode

play06:05

so go ahead and update your xcode to a

play06:07

newer version that supports

play06:09

16.4 let's jump back into our code and

play06:12

let's look at what the sheet looks like

play06:15

click me this is our typical sheet we

play06:18

can also do full screen

play06:21

cover and then we can also do the new

play06:25

popover now this is basically what we're

play06:27

really focused on in this video this is

play06:28

a cool little pop over that occurs right

play06:31

on The View where we put the modifier

play06:34

and this is important so when we use the

play06:36

the sheet right that sheet is coming up

play06:39

from basically anywhere on this screen

play06:42

that Sheet's going to come up from the

play06:43

bottom right if we put this code down

play06:46

here on the entire

play06:48

zstack the Sheet's going to look the

play06:50

exact same but when we use popover that

play06:54

popover is going to appear right on top

play06:56

of the view that it is drawn on so right

play06:59

now this popover is on this entire

play07:01

zstack we know this zstack is full

play07:04

screen because it's the entire gray area

play07:06

so if I click it you're probably going

play07:08

to wonder where on Earth is the popover

play07:10

happening right it clicks it but you

play07:12

can't really tell if you look really

play07:15

closely up here I'll try to zoom in here

play07:18

there's a little shadow that appears in

play07:20

this top

play07:22

corner it's really hard to see but

play07:24

basically this popover right now is

play07:26

appearing the anchor for the popover is

play07:29

in the top left corner of the entire

play07:31

zstack and so obviously that's not a

play07:34

great

play07:34

look but all we need to know is this

play07:37

popover is going to appear on The View

play07:39

that we're putting it on so if I move it

play07:41

and I put it on the button here then

play07:45

it's going to appear right on top of the

play07:47

button which is exactly what we want

play07:48

here before we move forward I do just

play07:50

want to throw out here that this very

play07:53

well may be the way that we start

play07:55

showing actual sheets and fulls screen

play07:57

covers in Swift UI so right now if I use

play08:01

the sheet modifier or the full screen

play08:03

cover modifier that's going to do more

play08:06

or less the exact same thing as a

play08:08

popover with the sheet adaptation and my

play08:12

guess is this is probably Apple subtly

play08:14

hinting to us that this is the direction

play08:16

that this framework might go and

play08:18

honestly in the long run it's probably

play08:19

going to be better because now we can

play08:22

customize which of those presentations

play08:24

we want just by changing this little

play08:27

enome here right so now I don't need to

play08:30

add a sheet and a full screen cover and

play08:32

a popover modifier I can just add this

play08:35

one popover and then just change what

play08:39

adaptation that I want here so kind of

play08:41

is going to open us up to basically

play08:43

being able to customize all of that much

play08:45

easier than we currently have apple has

play08:49

not officially said that but that's just

play08:50

my guess like otherwise I'm not really

play08:52

sure why they would put sheets inside

play08:55

this adaptation here we're probably

play08:58

going to start using Popo over more and

play08:59

more going forward anyway let's look

play09:02

again at the popover what we're really

play09:04

focused on in this video so the rest

play09:07

this video is just going to be playing

play09:08

around with the popover and maybe some

play09:10

of the different ways we can actually

play09:12

implement this now if I hold the option

play09:14

button and click on the popover modifier

play09:16

we can read a little bit of the

play09:17

documentation here and you're going to

play09:20

notice that in the complete function

play09:22

there is an attachment anchor as well as

play09:25

an arrow Edge and I'm come down here and

play09:27

read the attachment anchor

play09:29

is the positioning anchor that defines

play09:31

the attachment point of the popover so

play09:35

this is basically on the button where is

play09:37

the popover going to appear on top

play09:40

of and then we have Arrow Edge and if I

play09:42

read this it says the attachment anchor

play09:45

that defines the location in Mac OS so

play09:48

if we are not building for Mac OS which

play09:50

we are not right now the arrow Edge is

play09:52

not going to do anything so we're just

play09:55

going to play around with the attachment

play09:56

anchor real quick so let's go ahead and

play09:59

call popover again this time I'll hold

play10:02

the option button and click enter so we

play10:04

can get the actual completion here let's

play10:07

bind to show pop over and then for the

play10:09

attachment anchor I'm going to press the

play10:12

period and we can look at either a point

play10:14

or a rect a rect would be we're going to

play10:17

tell it the exact coordinates of where

play10:19

to appear but I think point is probably

play10:21

much easier to use much more common so

play10:23

let's go with that and let's. Center for

play10:26

now the arrow Edge we just discussed is

play10:28

not needed on iOS and then content let's

play10:31

click enter and just put our content

play10:33

back in this one and we will delete the

play10:36

old version here cool and on this button

play10:40

really quickly let's just add a little

play10:42

bit of padding and then maybe a

play10:44

background of color. yellow just so that

play10:48

I can see this Square let's make it

play10:50

maybe

play10:51

20 and again remember this popover is

play10:54

being drawn on top of the view that it

play10:57

is on so right now the pop pop over is

play10:59

on what looks like the yellow Square on

play11:01

the screen so if the attachment anchor

play11:03

is the center of that

play11:07

popover we can see this little arrow

play11:09

appearing right in the center of the

play11:11

view that it is on if I move this to

play11:14

maybe the top leading it's going to

play11:17

appear right in the top left corner of

play11:19

the view that is on and this is really

play11:21

cool super customizable so we can really

play11:24

customize how we want it to appear I

play11:26

think most of the time we probably want

play11:27

the popover to avoid

play11:29

the button that we are clicking on so if

play11:32

it's in the center it kind of covers up

play11:34

the text maybe that's what you want I

play11:36

would probably prefer top to the actual

play11:38

button

play11:40

Edge we could also move it to the bottom

play11:43

and now it's going to basically fully

play11:44

cover the button which I definitely do

play11:46

not like one thing I one thing I will

play11:49

point out here though is that the

play11:50

popover has this awesome dismissal that

play11:53

if you click anywhere on the screen

play11:54

it'll dismiss that popover and I think

play11:56

that's a really great ux that Apple's

play11:58

giv us out of the box but generally I

play12:01

don't want my pop over to cover my

play12:02

button like this so we can move it back

play12:04

to

play12:05

top now of course we can customize this

play12:07

attachment anchor but we can't actually

play12:10

customize where the popover is actually

play12:12

going to appear so you'll notice right

play12:15

now the anchor is at the top and then

play12:18

the popover is appearing above that

play12:20

anchor and if I move it to the bottom

play12:23

the now the anchor is at the bottom but

play12:26

the popover is still appearing on top of

play12:28

that anchor right so we can't actually

play12:31

customize where the popover is going to

play12:33

appear we can just customize the anchor

play12:35

at this time and you're probably

play12:37

wondering well that's a pretty bad ux

play12:39

why would Apple do that and that's

play12:41

because apple is including the logic to

play12:43

make sure that the popover appears

play12:45

within the screen itself so this is

play12:48

actually a benefit for us as developers

play12:51

so for example if I put this button in a

play12:53

vstack and then maybe I push the button

play12:56

to the top of the

play12:57

screen put a spacer in the bottom of the

play13:00

vstack if we click it now you'll notice

play13:03

the anchor is at the top but the actual

play13:05

popover is showing up below the

play13:08

anchor and that's because if the popover

play13:11

were to appear above the anchor it would

play13:13

be off screen and you just wouldn't be

play13:15

able to actually use it so Apple's

play13:17

actually customizing the logic to make

play13:19

sure that this popover is appearing on

play13:21

the screen which is super handy so

play13:24

generally speaking if the view that the

play13:26

popover is appearing on is towards the

play13:27

top of the screen

play13:29

like this my Anchor Point will probably

play13:31

be on the

play13:33

bottom but if it is at the bottom of the

play13:36

screen so if I put the spacer at the top

play13:39

and it's moved to the bottom of the

play13:40

screen my Anchor Point is probably going

play13:42

to be closer to the top all right and

play13:46

let's wrap this up with just maybe a

play13:47

real world example of when we might want

play13:49

to use some little popover like this so

play13:51

let's do an at State at State private

play13:54

bar let's say feedback options of type

play13:58

making an array of string we'll set it

play14:00

equal to an array here and let's just

play14:03

say maybe very good with a little Emoji

play14:06

because users love

play14:08

emojis let's do maybe

play14:11

average with another Emoji here maybe a

play14:14

little less happy just a little smiley

play14:16

face and then let's do maybe very bad

play14:20

and we'll do a little angry user face

play14:24

here and let's put these feedback

play14:27

options on the screen in this pop over

play14:29

here let's change out this text let's

play14:31

make it maybe a vstack let's make it

play14:33

alignment leading spacing maybe 12 and

play14:37

we'll do a for each in here for each of

play14:40

the feedback options which is an array

play14:42

of string and strings are hashable so

play14:46

the ID that we want to loop on is the

play14:49

hash value of the string

play14:51

itself so dot self is the hash value of

play14:55

the string just a quick reminder click

play14:57

enter here this will be for each option

play14:59

in the feedback options and now we're

play15:01

going to put another text on the screen

play15:03

that just has the

play15:06

option cool let's click it see where

play15:09

we're at all right let's maybe make each

play15:12

of these a button instead that has the

play15:15

option as the title and let's add a

play15:18

divider to each of these so let's click

play15:21

it again looking a little better we

play15:23

don't need the divider down below the

play15:25

bottom one so let's just say if option

play15:28

is is not equal to the feedback options.

play15:32

last then we'll put the divider on the

play15:34

screen and I want to add some padding

play15:36

outside of this entire popover so let's

play15:39

just add maybe some padding on the

play15:40

vstack let's do maybe

play15:43

20 and this is looking much

play15:47

better let's change the clickme to maybe

play15:50

provide

play15:51

feedback and now we have a super simple

play15:53

feedback popover in our app and if we

play15:56

had a lot of options we could even go

play15:58

ahead and put this inside like a scroll

play16:03

View and then we can have this awesome

play16:06

scroll view inside here with a whole

play16:07

bunch of options so I love this popover

play16:10

modifier just because a it's very easy

play16:12

to use it's using basically the same

play16:14

logic that we already know and love in

play16:16

Swift UI these super simple

play16:18

bindings all right so just wrapping up

play16:20

here before we end the video this

play16:23

popover the way to use it is basically

play16:25

the same way that we use a sheet or full

play16:27

screen cover like except we just have to

play16:29

add this new modifier here presentation

play16:32

compact adaptation a reminder that we

play16:34

can customize the adaptation for

play16:37

horizontal or vertical if we want so I'm

play16:40

just going to use popover on both of

play16:41

these just so we have the code

play16:44

here and that's it for this video just a

play16:47

quick one on how to use popover I think

play16:49

this is super powerful I'm going to be

play16:51

using this a lot in my apps I mean prior

play16:53

to this if you wanted to show a model

play16:55

like this it would require a lot of

play16:57

custom code and probably be not be as

play16:59

smooth and seamless as this native

play17:02

version right I can click anywhere on

play17:03

the screen to dismiss the popup and that

play17:05

is awesome and the popup is going to

play17:07

appear based based on where the button

play17:10

is on the screen so we saw when the

play17:11

button was at the top of the screen the

play17:13

popover appeared below the button now

play17:16

it's appearing on top of the button

play17:17

because it's at the bottom of the screen

play17:19

this is just a really good ux this is

play17:21

much better than having like segue to a

play17:23

new screen or show this big sheet right

play17:27

we can still show Sheets if if we want

play17:28

to but this is much more

play17:31

janky than just showing a quick little

play17:33

popover and I would say if you were just

play17:36

showing a little bit of code maybe a

play17:37

couple buttons maybe just a small little

play17:39

tip for the user this is a perfect way

play17:42

to do it anyway I hope you guys enjoyed

play17:44

the video as always my name is Nick this

play17:46

is swiftel thinking do not forget to hit

play17:48

the Subscribe button and I will see you

play17:51

in the next

play17:57

video

play18:01

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Swift UIiOS 17Popover模态视图信息提示用户界面教程编程AppleUX设计Swift编程
هل تحتاج إلى تلخيص باللغة الإنجليزية؟