How to use AnyLayout in SwiftUI | Bootcamp #70

Swiftful Thinking
9 Nov 202310:58

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

00:00

😀 Swift UI 布局协议简介

Nick 老师在视频中介绍了 Swift UI 中的布局协议,特别是 'AnyLayout' 的使用。他提到几乎所有的 Swift UI 视图都包含某种形式的 VStack 或 HStack,这些 Stacks 都遵循布局协议。Swift UI 提供了一种类型擦除的布局叫做 'AnyLayout',允许开发者使用任意布局而不是特定的 HStack 或 VStack。Nick 老师计划在未来的视频中更深入地探讨布局协议以及如何自定义它。本段视频主要面向初学者,展示了 'AnyLayout' 的基本概念以及如何在应用程序中使用它。

05:03

📱 设备方向与布局适应性

Nick 老师讨论了如何根据设备的方向调整布局。他提到了两个关键的环境变量:水平尺寸类和垂直尺寸类,并解释了它们在不同设备和方向上的表现。例如,iPhone Plus 在竖屏模式下垂直尺寸类是常规的,在横屏模式下则变为紧凑的。Nick 老师强调,作为开发者,应该更多地关注这些环境变量告诉我们的信息,而不是具体的设备类型。他通过一个简单的例子展示了如何根据尺寸类的不同来决定使用 VStack 或 HStack。

10:04

🤓 'AnyLayout' 的实际应用与简化

在讨论了布局适应性之后,Nick 老师引入了 'AnyLayout' 的概念,并展示了如何使用它来简化布局代码。他解释了 'AnyLayout' 是一种类型擦除的布局,可以代表任何可能的布局,如 VStack 或 HStack。通过使用 'AnyLayout',开发者可以根据水平尺寸类是紧凑还是常规,来决定使用垂直堆栈还是水平堆栈,从而使得布局代码更加简洁。Nick 老师还提到,对于初学者来说,使用 'AnyLayout' 来适应布局变化已经足够,尽管可以创建自定义布局并将其类型擦除为 'AnyLayout'。

Mindmap

Keywords

💡Swift UI

Swift UI 是苹果公司开发的一种用于创建用户界面的框架,它允许开发者使用Swift语言快速构建iOS、iPadOS、macOS、tvOS和watchOS的应用程序界面。在视频中,Swift UI 被用来介绍布局协议和如何使用 `AnyLayout` 来创建适应不同设备和方向的视图。

💡布局协议 (Layout Protocol)

布局协议是Swift UI中的一个核心概念,它定义了如何将视图的子元素进行排列。几乎所有的Swift UI视图都遵循这个协议,如VStack和HStack。视频提到了布局协议,但主要聚焦于介绍 `AnyLayout`。

💡VStack

VStack是Swift UI中的一个视图,代表垂直堆栈,用于垂直排列子视图。在视频中,VStack被用来创建一个垂直排列的文本视图,展示了如何根据设备的方向和大小调整布局。

💡HStack

HStack是Swift UI中的另一个视图,代表水平堆栈,用于水平排列子视图。视频通过HStack展示了如何根据设备的横向尺寸类别(horizontal size class)来改变布局,以适应更多的横向空间。

💡AnyLayout

AnyLayout是Swift UI中的一个类型擦除的布局,允许开发者创建一个可以是任何布局的视图。视频中通过 `AnyLayout` 简化了布局逻辑,使得代码更加简洁,并能够自动适应设备的方向和大小变化。

💡尺寸类别 (Size Class)

尺寸类别是Swift UI中用于描述设备屏幕尺寸和布局环境的概念。视频提到了水平尺寸类别(horizontal size class)和垂直尺寸类别(vertical size class),并解释了它们如何影响布局的选择。

💡类型擦除 (Type Erasure)

类型擦除是一种编程技术,允许将具体类型隐藏起来,只暴露其协议或接口。在视频中,通过 `AnyLayout` 使用了类型擦除,使得具体的布局类型(如VStack或HStack)被隐藏,只通过协议进行交互。

💡设备方向 (Device Orientation)

设备方向指的是用户使用设备时的物理方向,如竖屏(portrait)或横屏(landscape)。视频通过改变模拟器的方向来展示如何根据设备方向变化来调整布局。

💡环境变量 (Environment Variables)

环境变量是Swift UI中用于存储和访问关于设备和环境信息的变量。视频中提到了环境变量,如尺寸类别,它们会实时更新以反映设备状态的变化,从而允许布局自动适应这些变化。

💡条件语句 (Conditional Statements)

条件语句是一种编程结构,用于基于特定条件执行不同的代码分支。在视频中,使用了条件语句(如 `if` 和 `else`)来根据尺寸类别决定使用VStack还是HStack。

💡模拟器 (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

play00:00

welcome back everybody my name is Nick

play00:02

this is swiftel thinking and in this

play00:03

video we're going to take a very quick

play00:05

look at any layout in Swift UI this is

play00:08

basically an introduction to the layout

play00:10

protocol in Swift UI so when we're

play00:13

creating Swift UI views almost every

play00:16

view has some sort of vstack or H stack

play00:19

all of those Stacks conform to this

play00:21

layout protocol and Swift UI has a type

play00:24

erased layout called any layout that

play00:27

allows us to create views using any any

play00:30

layout rather than a specific H stack or

play00:32

a v stack so I'm going to do a video in

play00:34

the future that gets more into the

play00:36

actual layout protocol and how we can

play00:38

really customize it ourselves but in

play00:40

this video we're going to do a very

play00:41

beginner level look at what is any

play00:44

layout and how can I use it in my

play00:54

application all right welcome back

play00:56

everybody again in the Swift UI boot

play00:58

camp playlist it's going to be another

play01:00

quick video here some of these are

play01:02

really small features but I just want to

play01:03

get them on your guys's radar we're

play01:05

going to right click the Navigator here

play01:07

create a new file it'll be a swift UI

play01:09

View and we're going to call this one

play01:10

any layout boot camp let's click create

play01:14

let's jump inside real quick this should

play01:16

be a very fast one here any layout is a

play01:19

tool that we can use to basically adapt

play01:21

our layout depending on the device

play01:24

orientation and let's get into it I

play01:27

haven't touched on this very much in

play01:28

this playlist because um if you're a

play01:30

beginner I guess I don't think it's the

play01:32

first thing you should be worried about

play01:34

but in the environment um there's

play01:36

actually two key paths that we can use

play01:38

here I'll use the back slash period here

play01:41

and there is a horizontal size class

play01:45

let's make this a private variable

play01:47

called horizontal size class and let's

play01:50

do another one for the vertical size

play01:52

class and I'll call this vertical size

play01:53

class I haven't really touched on these

play01:55

much in this playlist but the size class

play01:58

for iPhone is either compact or regular

play02:02

and it's basically just the size of the

play02:03

screen that the view is on so if you

play02:06

just go and Google horizontal size class

play02:08

on iOS one of the top hits is this blog

play02:11

we're going to look at real quick from

play02:12

user loaf.com blogs siiz classes it's a

play02:16

great little read if you have some extra

play02:17

time but the bottom line of size glasses

play02:21

is basically that there is regular and

play02:23

there's Compact and if we look at this

play02:26

example here a iPhone plus model

play02:30

if it is in portrait mode the vertical

play02:32

size class would be regular and then if

play02:35

it's in landscape mode the vertical size

play02:38

class would be compact now this

play02:40

definitely gets a little confusing

play02:41

because different models are going to

play02:42

have different classes for example some

play02:45

devices might be in landscape mode and

play02:47

still have a regular height or vice

play02:50

versa but as a developer I think we

play02:52

should focus Less on whether or not the

play02:55

device is in landscape mode and more

play02:58

just on if if this class if apple is

play03:02

telling us that the device is compact we

play03:06

know it's going to have smaller space

play03:08

and then we can adapt our layout and so

play03:10

I focus Less on the actual devices here

play03:13

and more just on the these environment

play03:16

variables are going to tell us whether

play03:17

or not it's compact or regular so let's

play03:20

start this out with a hstack and maybe

play03:23

have add some spacing of 12 and I'm just

play03:26

going to put two texts on the screen

play03:27

let's just put in horizontal with a

play03:30

colon and let's just put the horizontal

play03:33

size class dot dis do debug description

play03:37

for now and I'm just going to do it for

play03:39

the vertical as well just so that we can

play03:41

see what these actually are and sorry

play03:44

let's make this a vstack all right so

play03:46

right now the

play03:47

horizontal the horizontal size class is

play03:50

compact the vertical size class is

play03:52

regular we can kind of assume that right

play03:55

like look at this device this the

play03:57

vertical is the regular it's the full

play03:59

height light and the horizontal is

play04:01

compact it's the shorter width of the

play04:04

device and we're going to and now we're

play04:06

going to draw some stuff on the screen

play04:07

here let's make another little vstack

play04:09

here and let's just put in text

play04:13

Alpha and I will copy that let's do beta

play04:16

let's do gamma cool all right so we have

play04:19

a vstack on the screen and and we're

play04:21

going to focus just on this tiny vstack

play04:23

here the alpha beta gamma we're not

play04:25

going to focus on the horizontal

play04:26

vertical I'm just going to leave that on

play04:28

the screen so that we can see

play04:30

uh the actual values now if this was

play04:33

your app and you were trying to make a

play04:34

different layout for whether or not the

play04:37

device was larger or smaller whether it

play04:39

was compact or regular you would add

play04:42

some cod to like if horizontal size

play04:44

class is equal to and we can see that

play04:47

it's either going to be regular or

play04:49

Compact and if it is compact then maybe

play04:53

we want to put this vstack on the screen

play04:56

cool and then we'll say else so if it is

play04:59

not compact right meaning it's regular

play05:02

the horizontal size class is regular

play05:04

meaning there's a lot of space

play05:05

horizontally so right now we can tell

play05:07

that there's not a lot of space

play05:09

horizontally and because of that we

play05:11

probably want to put our code in some

play05:12

sort of vstack but if it was the regular

play05:15

Dimension so probably if this device was

play05:17

in landscape mode we would have a lot of

play05:20

space and in that case we might want to

play05:23

put our layout in an

play05:25

hstack so let's look at a quick example

play05:27

here so I'm using right now an iPhone 14

play05:31

pro right and in the Pro if we are in

play05:34

portrait mode the horizontal is Compact

play05:38

and if I rotate it I'm going to click

play05:40

this little icon down here and go to

play05:41

orientation we're going to go to

play05:43

landscape we can say even in landscape

play05:46

on the iPhone 14 pro here the horizontal

play05:51

is still compact so this is one of those

play05:53

cases where it's not super intuitive you

play05:55

would think here we have more room but

play05:58

this is technically still a compact UI

play06:00

and so in all my compact uis we're going

play06:02

to have this vertical stack let's change

play06:05

this back to Portrait but this time I'm

play06:07

going to change my simulator so for

play06:09

example here if I switch my my simulator

play06:12

to iPhone 14 pro

play06:15

Max right this is a the Max has a larger

play06:18

screen size so if I switch this now to

play06:21

the landscape mode we can see in the

play06:25

horizontal size class in landscape mode

play06:27

on the larger device here is now regular

play06:31

so on the Mac sizes and also on like the

play06:33

iPads we're going to get the horizontal

play06:36

size as regular and we can see our code

play06:40

since it is now regular and not compact

play06:42

our Alpha Beta gamma is actually in a h

play06:45

stack which is pretty cool so this would

play06:47

be a better UI for the larger width of

play06:50

the device here because I have this

play06:52

space but on the compact version I want

play06:54

to keep it in a

play06:55

vstack so this is cool and we can write

play06:58

this code but what we're talking about

play07:00

in this video is just something called

play07:01

any layout and it's basically being able

play07:04

to write this a little bit cleaner

play07:06

because this is cool but if you get

play07:08

really complex examples like if you had

play07:11

entire screens inside here and inside

play07:13

here it might get really maybe tricky to

play07:16

work with so now what we're going to do

play07:18

is basically just write this code a

play07:20

little bit cooler um by using something

play07:22

called any layout when it comes to the

play07:24

Swift language uh generally when we see

play07:26

this word any before something it

play07:29

implies that it's a type eraser of

play07:31

something now I haven't covered what

play07:33

type erasers are in this beginner

play07:35

playlist and I don't think you really

play07:36

need to know what they are but it

play07:38

basically just means that any

play07:41

layout is something that could be any of

play07:46

the possible layouts and so in this case

play07:49

layout is actually a protocol vstack is

play07:52

a layout and H stack is a layout and

play07:55

this any layout is just saying we're

play07:57

going to give it a piece of code that

play07:58

could be any layout and so the closure

play08:02

here and the closure here are the exact

play08:05

same so either of those could work for

play08:07

any

play08:08

layout so what we're going to do really

play08:10

simply we're going to say let layout of

play08:12

type any layout and we're going to set

play08:14

it equal to any layout and then you'll

play08:17

see in here we have to pass in a layout

play08:20

now if I start typing in vstack you'll

play08:22

see that v stack layout is the official

play08:25

layout of the vertical stack so I'll

play08:28

I'll press enter here vstack layout open

play08:31

and close parentheses so now this is

play08:33

actually type any layout but we know

play08:36

it's actually going to be a vstack

play08:38

layout and so I can put this on the

play08:40

screen now with layout and open the

play08:43

brackets so the same thing that we have

play08:44

here this vstack with the brackets I'm

play08:47

going to now just put this code inside

play08:50

here so now we have any layout and this

play08:53

will be the content inside that layout

play08:56

all right and now obviously we want to

play08:57

still use this this logic here so if the

play08:59

horizontal size class is compact then we

play09:02

want the vertical layout otherwise we

play09:03

want the horizontal layout so I'm just

play09:06

going to use a tary operator here I'm

play09:07

going to say let layout of type any

play09:09

layout set it equal to if the horizontal

play09:12

size class is equal to compact question

play09:15

mark then we're going to use a vertical

play09:18

layout that is erased to any layout

play09:22

otherwise let's use any layout with an

play09:25

hstack layout so if the horizontal size

play09:28

class is compact it should be a v stack

play09:30

otherwise an H stack I'll zoom out a

play09:33

little bit just so this is on one line

play09:36

and now we're just going to comment this

play09:37

out of our

play09:38

code we're just going to have layout

play09:40

here and we're going to look at the

play09:43

simulator and we can see that the

play09:44

horizontal size class is regular right

play09:46

now so it's in an H stack and if I

play09:50

switch back to the portrait mode

play09:53

horizontal is Compact and we're in a

play09:54

vstack that's all we're going to learn

play09:56

in this video there are ways to make

play09:58

your own layouts and then you can type

play10:00

erase your custom layouts to any layout

play10:03

but generally speaking especially for

play10:05

beginner apps I think this is basically

play10:07

the maximum of the logic that you're

play10:08

going to need you're just taking

play10:10

whatever the layout is erasing it so

play10:12

that we can call it once in our code I

play10:14

think this is super cool it's awesome

play10:16

that it adapts in real time so if the

play10:17

user is like moving your app or rotating

play10:19

your app this layout will adapt

play10:22

automatically immediately because these

play10:24

environment variables get updated in

play10:26

real

play10:27

time but I just wanted to show you guys

play10:29

that I don't use this that often if you

play10:31

can have a single layout that just works

play10:33

um without having to adapt to the

play10:35

changing device that's probably better

play10:37

but if you need to adapt now you know

play10:39

how that's it for this video thank you

play10:41

guys for watching as always I am Nick

play10:43

this is swiftel thinking and I will see

play10:45

you in the next

play10:51

[Music]

play10:56

video

Rate This

5.0 / 5 (0 votes)

Related Tags
Swift UI布局协议AnyLayout设备方向屏幕尺寸自适应布局编程教程iPhone 14开发者指南视频教学初学者用户界面
Do you need a summary in English?