How to use AnyLayout in SwiftUI | Bootcamp #70
Summary
TLDR在这段视频脚本中,Nick介绍了Swift UI中的“Any Layout”概念。他首先解释了Swift UI视图中常见的VStack和HStack都遵循了布局协议,而Any Layout是一个类型擦除的布局,允许开发者使用任何布局而非特定的垂直或水平堆叠。Nick通过创建一个名为“Any Layout Boot Camp”的Swift UI视图,展示了如何使用水平和垂直尺寸类来根据不同设备的方向调整布局。他演示了在紧凑和常规尺寸类下,如何使用if语句来决定是使用垂直堆叠(VStack)还是水平堆叠(HStack)。最后,他展示了如何使用Any Layout来简化代码,使得布局能够自动适应不同的屏幕尺寸和方向,从而实现更灵活的用户界面设计。
Takeaways
- 📘 Swift UI 中的 `AnyLayout` 是一个布局协议,允许开发者使用任何布局而不仅仅是特定的 `HStack` 或 `VStack`。
- 🔄 `AnyLayout` 是一个类型擦除的布局,意味着它可以代表任何可能的布局类型。
- 📱 设备的方向和屏幕尺寸会影响布局,Swift UI 通过 `horizontalSizeClass` 和 `verticalSizeClass` 来识别这些属性。
- 📊 `horizontalSizeClass` 可以是 `compact` 或 `regular`,这取决于设备在水平方向上的空间大小。
- 📈 `verticalSizeClass` 同样可以是 `compact` 或 `regular`,取决于设备在垂直方向上的空间大小。
- 🔧 开发者可以根据 `horizontalSizeClass` 的值来决定使用 `VStack` 还是 `HStack`,以适应不同的屏幕尺寸和方向。
- 📱 在实际应用中,通过检查 `horizontalSizeClass` 的值,可以动态地在 `VStack` 和 `HStack` 之间切换以优化布局。
- 🌐 通过 `AnyLayout`,可以简化布局代码,使其更加清晰和易于维护。
- 🛠️ 对于初学者来说,了解 `AnyLayout` 和如何根据设备方向和屏幕尺寸调整布局是重要的,但通常不需要过于复杂。
- 🤖 如果应用的布局可以在不同设备上通用而无需调整,那么使用单一布局可能更为合适。
- 🔄 `AnyLayout` 使得布局能够实时自动适应用户的操作,如移动或旋转设备,因为环境变量会实时更新。
Q & A
Swift UI 中的 'AnyLayout' 是什么?
-在Swift UI中,'AnyLayout' 是一个协议,它允许创建使用任意布局的视图,而不仅仅是特定的HStack或VStack。它是一个类型擦除的布局,可以简化布局代码的编写。
Swift UI 的布局协议有哪些用途?
-Swift UI的布局协议用于定义视图的布局行为。几乎所有的Swift UI视图都以某种形式使用VStack或HStack,这些Stacks都遵循布局协议,使得开发者能够创建和自定义布局。
如何使用Swift UI中的水平和垂直尺寸类来适应不同设备的方向?
-通过检查环境变量中的水平和垂直尺寸类,可以判断设备是处于紧凑模式还是常规模式。紧凑模式意味着屏幕空间较小,而常规模式则表示有更多的空间。根据这些信息,开发者可以决定使用VStack或HStack来适应不同的布局需求。
在Swift UI中,如何根据设备的尺寸类动态改变布局?
-可以使用Swift UI的条件语句来检查水平尺寸类,如果尺寸类是紧凑的,则使用VStack;如果是常规的,则使用HStack。这样可以使布局根据设备的宽度自动调整。
Swift UI中的 'AnyLayout' 如何帮助简化布局代码?
-通过使用 'AnyLayout',开发者可以将特定的布局(如VStack或HStack)封装进一个类型擦除的布局中,这样就可以在代码中统一处理不同的布局,而不需要重复编写条件逻辑。
为什么在某些情况下,不使用 'AnyLayout' 可能更好?
-如果一个单一的布局就能够适应不同的设备和方向,而不需要复杂的适配逻辑,那么直接使用VStack或HStack可能更简单、更清晰。'AnyLayout' 更适用于需要根据设备尺寸类频繁改变布局的场景。
如何在Swift UI中创建一个新的Swift UI视图文件?
-在Swift UI的Navigator中,可以右键点击并选择创建一个新的文件,选择Swift UI View,然后给文件命名,例如 'AnyLayoutBootCamp'。
Swift UI中的环境变量是如何实时更新的?
-Swift UI中的环境变量,如水平和垂直尺寸类,会根据用户的交互实时更新,例如当用户旋转设备时,这些变量会立即反映出新的方向和尺寸信息。
如何在Swift UI中查看设备的水平和垂直尺寸类?
-可以通过在Swift UI视图中使用水平尺寸类和垂直尺寸类的debugDescription属性来查看当前的尺寸类状态。
Swift UI的布局协议和 'AnyLayout' 有什么关系?
-布局协议定义了Swift UI中布局的行为,而 'AnyLayout' 是一个实现该协议的类型擦除布局,它允许开发者将任何布局抽象化,从而在代码中统一处理。
在Swift UI中,如何使用 'AnyLayout' 来根据设备的方向改变布局?
-通过在 'AnyLayout' 的初始化中使用条件语句,根据水平尺寸类是紧凑还是常规,来决定传递给 'AnyLayout' 的是VStack布局还是HStack布局。
为什么在Swift UI中,即使在横屏模式下,某些设备的尺寸类可能仍然是紧凑的?
-这是因为尺寸类的判断不仅基于屏幕的方向,还基于屏幕的实际大小。即使在横屏模式下,如果屏幕的宽度不足以容纳更多的内容,尺寸类可能仍然是紧凑的。
Outlines
😀 Swift UI 布局协议简介
Nick 老师在视频中介绍了 Swift UI 中的布局协议,特别是 'AnyLayout' 的使用。他提到几乎所有的 Swift UI 视图都包含某种形式的 VStack 或 HStack,这些 Stacks 都遵循布局协议。Swift UI 提供了一种类型擦除的布局叫做 'AnyLayout',允许开发者使用任意布局而不是特定的 HStack 或 VStack。Nick 老师计划在未来的视频中更深入地探讨布局协议以及如何自定义它。本段视频主要面向初学者,展示了 'AnyLayout' 的基本概念以及如何在应用程序中使用它。
📱 设备方向与布局适应性
Nick 老师讨论了如何根据设备的方向调整布局。他提到了两个关键的环境变量:水平尺寸类和垂直尺寸类,并解释了它们在不同设备和方向上的表现。例如,iPhone Plus 在竖屏模式下垂直尺寸类是常规的,在横屏模式下则变为紧凑的。Nick 老师强调,作为开发者,应该更多地关注这些环境变量告诉我们的信息,而不是具体的设备类型。他通过一个简单的例子展示了如何根据尺寸类的不同来决定使用 VStack 或 HStack。
🤓 'AnyLayout' 的实际应用与简化
在讨论了布局适应性之后,Nick 老师引入了 'AnyLayout' 的概念,并展示了如何使用它来简化布局代码。他解释了 'AnyLayout' 是一种类型擦除的布局,可以代表任何可能的布局,如 VStack 或 HStack。通过使用 'AnyLayout',开发者可以根据水平尺寸类是紧凑还是常规,来决定使用垂直堆栈还是水平堆栈,从而使得布局代码更加简洁。Nick 老师还提到,对于初学者来说,使用 'AnyLayout' 来适应布局变化已经足够,尽管可以创建自定义布局并将其类型擦除为 'AnyLayout'。
Mindmap
Keywords
💡Swift UI
💡布局协议 (Layout Protocol)
💡VStack
💡HStack
💡AnyLayout
💡尺寸类别 (Size Class)
💡类型擦除 (Type Erasure)
💡设备方向 (Device Orientation)
💡环境变量 (Environment Variables)
💡条件语句 (Conditional Statements)
💡模拟器 (Simulator)
Highlights
Nick介绍了Swift UI中的布局协议,几乎所有视图都包含某种形式的VStack或HStack,它们都遵循这个协议。
Swift UI提供了一个类型擦除的布局叫做AnyLayout,允许我们使用任何布局而不是特定的HStack或VStack。
介绍了如何使用AnyLayout来创建视图,并通过Swift UI Boot Camp播放列表进行演示。
讨论了设备方向和布局自适应,包括如何使用水平和垂直尺寸类来调整布局。
通过一个实际的例子,展示了如何在iPhone不同型号和方向下,根据尺寸类调整布局。
解释了在iPhone Plus模型中,肖像模式下垂直尺寸类是常规的,而在风景模式下是紧凑的。
强调了作为开发者,应该更多关注空间大小而不是设备的具体方向。
演示了如何使用HStack和VStack以及如何通过尺寸类来决定使用哪种布局。
展示了在iPhone 14 Pro上,即使在风景模式下,水平尺寸类仍然是紧凑的。
说明了在更大的设备上,如iPhone 14 Pro Max,在风景模式下水平尺寸类是常规的。
介绍了如何使用AnyLayout来简化代码,使其更加清晰地处理不同布局。
通过一个条件语句,展示了如何根据水平尺寸类是紧凑还是常规来决定使用VStack或HStack。
强调了AnyLayout的实用性,它可以实时适应用户的设备方向变化。
提到了对于初学者来说,理解并应用AnyLayout是一个很好的起点,但更复杂的自定义布局可能需要更深入的学习。
Nick建议,如果一个单一的布局可以适应所有情况,那么可能不需要使用AnyLayout。
视频最后,Nick感谢观众观看,并预告了下一期视频的内容。
Transcripts
welcome back everybody my name is Nick
this is swiftel thinking and in this
video we're going to take a very quick
look at any layout in Swift UI this is
basically an introduction to the layout
protocol in Swift UI so when we're
creating Swift UI views almost every
view has some sort of vstack or H stack
all of those Stacks conform to this
layout protocol and Swift UI has a type
erased layout called any layout that
allows us to create views using any any
layout rather than a specific H stack or
a v stack so I'm going to do a video in
the future that gets more into the
actual layout protocol and how we can
really customize it ourselves but in
this video we're going to do a very
beginner level look at what is any
layout and how can I use it in my
application all right welcome back
everybody again in the Swift UI boot
camp playlist it's going to be another
quick video here some of these are
really small features but I just want to
get them on your guys's radar we're
going to right click the Navigator here
create a new file it'll be a swift UI
View and we're going to call this one
any layout boot camp let's click create
let's jump inside real quick this should
be a very fast one here any layout is a
tool that we can use to basically adapt
our layout depending on the device
orientation and let's get into it I
haven't touched on this very much in
this playlist because um if you're a
beginner I guess I don't think it's the
first thing you should be worried about
but in the environment um there's
actually two key paths that we can use
here I'll use the back slash period here
and there is a horizontal size class
let's make this a private variable
called horizontal size class and let's
do another one for the vertical size
class and I'll call this vertical size
class I haven't really touched on these
much in this playlist but the size class
for iPhone is either compact or regular
and it's basically just the size of the
screen that the view is on so if you
just go and Google horizontal size class
on iOS one of the top hits is this blog
we're going to look at real quick from
user loaf.com blogs siiz classes it's a
great little read if you have some extra
time but the bottom line of size glasses
is basically that there is regular and
there's Compact and if we look at this
example here a iPhone plus model
if it is in portrait mode the vertical
size class would be regular and then if
it's in landscape mode the vertical size
class would be compact now this
definitely gets a little confusing
because different models are going to
have different classes for example some
devices might be in landscape mode and
still have a regular height or vice
versa but as a developer I think we
should focus Less on whether or not the
device is in landscape mode and more
just on if if this class if apple is
telling us that the device is compact we
know it's going to have smaller space
and then we can adapt our layout and so
I focus Less on the actual devices here
and more just on the these environment
variables are going to tell us whether
or not it's compact or regular so let's
start this out with a hstack and maybe
have add some spacing of 12 and I'm just
going to put two texts on the screen
let's just put in horizontal with a
colon and let's just put the horizontal
size class dot dis do debug description
for now and I'm just going to do it for
the vertical as well just so that we can
see what these actually are and sorry
let's make this a vstack all right so
right now the
horizontal the horizontal size class is
compact the vertical size class is
regular we can kind of assume that right
like look at this device this the
vertical is the regular it's the full
height light and the horizontal is
compact it's the shorter width of the
device and we're going to and now we're
going to draw some stuff on the screen
here let's make another little vstack
here and let's just put in text
Alpha and I will copy that let's do beta
let's do gamma cool all right so we have
a vstack on the screen and and we're
going to focus just on this tiny vstack
here the alpha beta gamma we're not
going to focus on the horizontal
vertical I'm just going to leave that on
the screen so that we can see
uh the actual values now if this was
your app and you were trying to make a
different layout for whether or not the
device was larger or smaller whether it
was compact or regular you would add
some cod to like if horizontal size
class is equal to and we can see that
it's either going to be regular or
Compact and if it is compact then maybe
we want to put this vstack on the screen
cool and then we'll say else so if it is
not compact right meaning it's regular
the horizontal size class is regular
meaning there's a lot of space
horizontally so right now we can tell
that there's not a lot of space
horizontally and because of that we
probably want to put our code in some
sort of vstack but if it was the regular
Dimension so probably if this device was
in landscape mode we would have a lot of
space and in that case we might want to
put our layout in an
hstack so let's look at a quick example
here so I'm using right now an iPhone 14
pro right and in the Pro if we are in
portrait mode the horizontal is Compact
and if I rotate it I'm going to click
this little icon down here and go to
orientation we're going to go to
landscape we can say even in landscape
on the iPhone 14 pro here the horizontal
is still compact so this is one of those
cases where it's not super intuitive you
would think here we have more room but
this is technically still a compact UI
and so in all my compact uis we're going
to have this vertical stack let's change
this back to Portrait but this time I'm
going to change my simulator so for
example here if I switch my my simulator
to iPhone 14 pro
Max right this is a the Max has a larger
screen size so if I switch this now to
the landscape mode we can see in the
horizontal size class in landscape mode
on the larger device here is now regular
so on the Mac sizes and also on like the
iPads we're going to get the horizontal
size as regular and we can see our code
since it is now regular and not compact
our Alpha Beta gamma is actually in a h
stack which is pretty cool so this would
be a better UI for the larger width of
the device here because I have this
space but on the compact version I want
to keep it in a
vstack so this is cool and we can write
this code but what we're talking about
in this video is just something called
any layout and it's basically being able
to write this a little bit cleaner
because this is cool but if you get
really complex examples like if you had
entire screens inside here and inside
here it might get really maybe tricky to
work with so now what we're going to do
is basically just write this code a
little bit cooler um by using something
called any layout when it comes to the
Swift language uh generally when we see
this word any before something it
implies that it's a type eraser of
something now I haven't covered what
type erasers are in this beginner
playlist and I don't think you really
need to know what they are but it
basically just means that any
layout is something that could be any of
the possible layouts and so in this case
layout is actually a protocol vstack is
a layout and H stack is a layout and
this any layout is just saying we're
going to give it a piece of code that
could be any layout and so the closure
here and the closure here are the exact
same so either of those could work for
any
layout so what we're going to do really
simply we're going to say let layout of
type any layout and we're going to set
it equal to any layout and then you'll
see in here we have to pass in a layout
now if I start typing in vstack you'll
see that v stack layout is the official
layout of the vertical stack so I'll
I'll press enter here vstack layout open
and close parentheses so now this is
actually type any layout but we know
it's actually going to be a vstack
layout and so I can put this on the
screen now with layout and open the
brackets so the same thing that we have
here this vstack with the brackets I'm
going to now just put this code inside
here so now we have any layout and this
will be the content inside that layout
all right and now obviously we want to
still use this this logic here so if the
horizontal size class is compact then we
want the vertical layout otherwise we
want the horizontal layout so I'm just
going to use a tary operator here I'm
going to say let layout of type any
layout set it equal to if the horizontal
size class is equal to compact question
mark then we're going to use a vertical
layout that is erased to any layout
otherwise let's use any layout with an
hstack layout so if the horizontal size
class is compact it should be a v stack
otherwise an H stack I'll zoom out a
little bit just so this is on one line
and now we're just going to comment this
out of our
code we're just going to have layout
here and we're going to look at the
simulator and we can see that the
horizontal size class is regular right
now so it's in an H stack and if I
switch back to the portrait mode
horizontal is Compact and we're in a
vstack that's all we're going to learn
in this video there are ways to make
your own layouts and then you can type
erase your custom layouts to any layout
but generally speaking especially for
beginner apps I think this is basically
the maximum of the logic that you're
going to need you're just taking
whatever the layout is erasing it so
that we can call it once in our code I
think this is super cool it's awesome
that it adapts in real time so if the
user is like moving your app or rotating
your app this layout will adapt
automatically immediately because these
environment variables get updated in
real
time but I just wanted to show you guys
that I don't use this that often if you
can have a single layout that just works
um without having to adapt to the
changing device that's probably better
but if you need to adapt now you know
how that's it for this video thank you
guys for watching as always I am Nick
this is swiftel thinking and I will see
you in the next
[Music]
video
関連動画をさらに表示
How to use Group in SwiftUI | Bootcamp #66
How to use Popover modifier in SwiftUI | Bootcamp #69
Adding an App Icon and Launch Screen to SwiftUI | Todo List #7
How to create resizable sheets in SwiftUI | Bootcamp #64
How to use NavigationStack in SwiftUI | Bootcamp #62
How to use Toolbar in SwiftUI | Bootcamp #63
5.0 / 5 (0 votes)