How to add Badges to SwiftUI TabView and List in iOS 15 | Bootcamp #59

Swiftful Thinking
13 Dec 202105:04

Summary

TLDR在这个视频中,我们学习了如何在Swift UI应用中添加徽章(Badges)。尽管徽章在iOS 15之前并未包含在Swift UI中,但它们已经在UIKit中存在了很长时间。视频中,我们首先探讨了如何在标签栏(Tab Bar)中添加徽章,以显示每个标签的通知数量。通过简单地设置一个数字或字符串,我们可以轻松地更新徽章的计数,并且如果计数为零,徽章将不会显示。此外,视频还展示了如何在列表(List)中使用徽章,虽然列表中的徽章外观与标签栏中的不同,但它们提供了一种在列表项旁边显示额外信息的方法。整个教程简洁明了,由Nick通过他的频道Swiffle Thinking呈现。

Takeaways

  • 📘 Swift UI 直到 iOS 15 才引入了徽章(Badges),之前在 UIKit 中已有此功能。
  • 📌 徽章在 Swift UI 中主要用于列表行(List Rows)和标签栏(Tab Bars)。
  • 🔲 通过在 TabItem 中添加一个圆点徽章(Dot Badge),可以显示特定标签上的通知数量。
  • 📈 徽章的计数(Count)可以是数字,也可以是字符串,如“new”。
  • 🚫 如果徽章的计数为零,则不会显示,避免了无通知时的误显示。
  • 🎨 徽章在标签栏中的颜色会根据项目中的强调色(Accent Color)变化。
  • 📝 在列表中,徽章显示为右侧的内容,并且使用次要文本颜色,以区别于主要文本。
  • 🔄 Swift UI 中的徽章可以方便地更新,只需更改计数即可自动更新显示。
  • 📱 徽章在列表和标签视图中的使用,增强了应用程序的交互性和信息展示。
  • 📑 视频教程提供了如何将徽章添加到 Swift UI 应用程序的步骤和示例。
  • 👤 视频由 Nick 提供,他是 Swiffle Thinking 的一部分,专注于 Swift UI 的教学。

Q & A

  • Swift UI 中的徽章(Badges)最初是在哪个版本的 iOS 中引入的?

    -Swift UI 中的徽章(Badges)是在 iOS 15 中引入的。

  • 在 Swift UI 中,徽章可以用于哪些 UI 组件?

    -在 Swift UI 中,徽章可以用于列表行(List Rows)和标签栏(Tab Bars)。

  • 如何为 Swift UI 的标签栏添加一个点状徽章?

    -为 Swift UI 的标签栏添加一个点状徽章,可以通过设置 `TabItem` 的 `badge` 属性,并为其提供计数(如数字 5)来实现。

  • 如果徽章的计数为零,它在界面上会如何显示?

    -如果徽章的计数为零,它将不会在界面上显示。

  • 除了数字,徽章是否也可以显示字符串?

    -是的,除了数字,徽章也可以显示字符串,例如可以显示 'new' 这样的文本。

  • 在 Swift UI 的列表中,徽章的外观与传统的点状徽章有何不同?

    -在 Swift UI 的列表中,徽章的外观与传统的点状徽章不同,它不是小的红色图标,而是作为右侧的内容显示,使用辅助文本颜色。

  • 在 Swift UI 的列表中添加徽章时,通常推荐使用哪种类型的徽章内容?

    -在 Swift UI 的列表中添加徽章时,通常推荐使用文本类型的徽章内容,因为这样更相关。

  • 如何更新 Swift UI 中徽章的计数?

    -在 Swift UI 中,更新徽章的计数与更新其他任何 UI 组件一样简单,只需更改 `badge` 属性的计数即可,徽章会自动更新。

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

    -在 Xcode 中,可以通过右键点击导航器,选择 'New File',然后选择 'Swift UI View' 并命名文件来创建一个新的 Swift UI 视图文件。

  • 在 Swift UI 中,如何设置标签栏的背景颜色?

    -在 Swift UI 中,标签栏的背景颜色是通过设置项目的 Accent Color 来实现的,这会影响标签栏的颜色。

  • 在 Swift UI 中,如何为标签栏的每个标签项设置不同的颜色?

    -在 Swift UI 中,为标签栏的每个标签项设置颜色是通过在创建 `TabView` 时为每个 `TabItem` 设置颜色来实现的。

  • 在 Swift UI 中,如何使用系统图标作为标签项的图像?

    -在 Swift UI 中,可以通过 `TabItem` 的初始化器使用系统名称(system name)来设置标签项的图像,例如使用 `heart.fill`。

Outlines

00:00

📱 Swift UI 应用中添加徽章

本段介绍了如何在 Swift UI 应用中添加徽章,这是 iOS 15 新增的功能。徽章可以用来在 Tab Bar 上显示通知数量,类似于 UIKit 中的长期功能。视频将展示如何在 Xcode 中创建一个简单的 Swift UI 视图,并在 Tab Bar 上添加徽章。

Mindmap

Keywords

💡Swift UI

Swift UI是一个由苹果公司开发,用于在iOS、iPadOS、macOS、watchOS和tvOS上创建用户界面的框架。它允许开发者使用Swift语言和声明式语法来设计应用程序的界面。在视频中,Swift UI是创建和自定义用户界面的核心主题,特别是在添加徽章(Badges)的部分。

💡徽章(Badges)

徽章在用户界面设计中通常用来表示未读的通知数量或状态。在视频中,徽章被用来在Swift UI的Tab Bar和List Rows中显示通知或状态更新,例如在Tab Bar上显示数字5表示有五个未读通知。

💡Tab Bar

Tab Bar是一种常见的用户界面组件,它允许用户在不同的视图或功能模块间切换。在视频中,Tab Bar被用来创建具有不同功能的多个标签页,并且通过在Tab Bar上添加徽章来指示每个标签页的通知数量。

💡List Rows

List Rows是列表视图中的单个项目,通常用来展示数据项。视频中提到,在Swift UI中,徽章也可以添加到List Rows上,用来展示列表项的特定状态或信息。

💡Xcode

Xcode是苹果公司开发的集成开发环境(IDE),用于开发macOS、iOS、watchOS和tvOS的应用程序。视频中提到使用Xcode来创建和编辑Swift UI应用程序,说明了Xcode在开发过程中的重要性。

💡系统名称(System Name)

在Swift UI中,系统名称指的是一组预定义的图标名称,可以用来快速访问和使用苹果设计的语言图标。视频中提到使用系统名称heart.fill来作为Tab Bar的一个图标,展示了如何利用系统图标来增强界面的视觉效果。

💡Accent Color

Accent Color是指在用户界面中用来强调特定元素的颜色,如按钮、链接或选中状态的背景色。视频中提到项目中的Accent Color是橙色,这影响了Tab Bar的颜色,说明了Accent Color在界面设计中的作用。

💡Tab View

Tab View是Swift UI中的一个视图,用来以标签页的形式展示内容。视频中通过创建一个简单的Tab View来展示如何使用Swift UI构建具有多个标签页的界面。

💡声明式语法

声明式语法是一种编程范式,它允许开发者通过声明组件和它们的性质来构建用户界面,而不是通过命令式的步骤来操作界面。Swift UI就是使用声明式语法来设计界面,视频中通过添加徽章来展示了声明式语法的用法。

💡更新(Update)

在软件开发中,更新指的是对现有代码或界面进行修改以添加新功能或修复问题。视频中提到可以更新徽章上的数字,就像更新Swift UI中的其他任何元素一样,强调了Swift UI的动态和响应式特性。

💡Nick

Nick是视频中的讲述者和教学者的名字。在视频的结尾,Nick以他的名字作为标识,结束了教学视频,这有助于建立与观众的个人联系,并增加了视频的亲切感。

Highlights

Swift UI 现在可以添加徽章(Badges),这在iOS 15之前是不支持的。

徽章可以用来在标签栏(Tab Bar)上显示通知数量。

在Swift UI中,徽章只能显示在列表行(List Rows)和标签栏(Tab Bars)上。

创建一个新的Swift UI视图文件,名为'Badges Bootcamp'。

通过在Tab Bar上添加点状徽章并设置计数为5,可以轻松添加徽章。

如果徽章计数为零,则不会显示,这避免了无通知时徽章的出现。

徽章不仅可以使用数字,还可以使用字符串,如'new'。

在列表中,徽章看起来与标签栏上的徽章不同,它显示为右侧的次要文本颜色内容。

列表中的徽章可以显示为“新项目”等文本,以提供更多信息。

徽章在列表中显示时,颜色会根据项目的主要和次要颜色变化。

在Swift UI中更新徽章的计数或文本,徽章会自动更新。

视频中演示了如何在Xcode中创建一个简单的标签栏,并为其添加徽章。

标签栏的颜色会根据项目中的强调色(Accent Color)变化。

创建了一个包含红、绿、蓝三种颜色的简单标签栏。

每个标签项(Tab Item)都有一个系统名称的图片和文本。

通过复制和粘贴,可以快速为所有三个标签项添加相同的设置。

视频由Nick制作,属于Swiffle Thinking系列。

观看视频可以学习如何在Swift UI中使用徽章,这在iOS应用开发中非常有用。

Transcripts

play00:00

[Music]

play00:04

what's up everyone another quick video

play00:06

coming at you guys today we're gonna

play00:08

look at how we can add badges uh into

play00:10

our swift ui apps now

play00:13

badges probably should have been in the

play00:15

first iteration of swift ui i don't know

play00:16

why apple didn't include this the first

play00:18

time but basically when we have a tab

play00:20

bar and we want to put a little icon

play00:22

indicating how maybe how many

play00:24

notifications are on that tab we do that

play00:26

using a batch we've had badges in ui kit

play00:29

for a long time now but we did not have

play00:31

them in swift ui until ios 15. so in

play00:35

this video we're going to take a quick

play00:36

look at how we can add a badge we're

play00:38

also going to look at how we can add

play00:39

badges or badges uh to lists so we can

play00:43

add them to lists they look a little

play00:44

different not like a typical badge it's

play00:46

still called badge though

play00:48

uh but still a nice feature to have so

play00:51

let's jump into xcode and take a look

play00:55

all right we are back in our xcode

play00:57

project let's right click the navigator

play00:59

create a new file it'll be a swift ui

play01:02

view and let's call this one

play01:04

badges bootcamp

play01:07

let's click create

play01:10

let's click resume on the canvas to make

play01:12

sure we are connected here and

play01:15

uh this one again will be a pretty

play01:17

simple video i wish these had come out

play01:19

when i first created this boot camp

play01:21

because i would have included them in

play01:22

the original videos of course

play01:24

and there are two places that we can use

play01:26

badges in swift ui if uh if we call that

play01:29

badge here we can add an account it's

play01:32

not going to do anything but if we right

play01:33

click and jump to the definition of

play01:34

badge

play01:35

at least as of today when i'm making

play01:38

this

play01:39

we can see that badges are only

play01:41

displayed in list rows and tab bars

play01:46

so we can i guess add badge anywhere in

play01:48

our code but it's really only going to

play01:50

work on a list or a tab view

play01:53

so let's write that down list and tab it

play01:56

said tab bar but really we're gonna have

play01:58

to use a tab view

play02:00

and if you are just joining i did cover

play02:02

tab view in its own series earlier in

play02:04

its own video earlier in this series so

play02:06

we have a whole video on tab view if you

play02:08

don't know how to use it but let's

play02:09

create a really simple one we'll just

play02:11

create a a blank tab view here open the

play02:14

brackets

play02:15

inside i'm just going to put let's do

play02:17

color.red

play02:18

color.color.green

play02:22

and color.blue these will be our three

play02:24

tabs each tab is gonna have a tab

play02:27

item

play02:28

click enter tab item we'll have an image

play02:31

we'll use a system name

play02:33

let's do heart.fill and a text that says

play02:36

let's just say hello for a second

play02:39

and we can see the tabs coming in here

play02:41

and i'm going to just copy and paste

play02:44

this tab item on all three

play02:47

and i want to point out again that the

play02:49

color in the tab bar is the color of the

play02:51

accent color in your project so my

play02:53

project right now has this orange tint

play02:55

if i go to the access if i go to the

play02:57

assets folder that's because our accent

play02:58

color is this orange

play03:00

if i were to get rid of this color

play03:03

come on back we'll get back to our

play03:05

native default blue

play03:07

uh but this video is all about the

play03:09

badges so really simply we can add a dot

play03:12

badge

play03:13

and we're gonna go down to the count

play03:15

option and let's just put in the number

play03:16

five

play03:18

and it's really that simple now we can

play03:20

add badges to our tab bar which is so

play03:23

convenient

play03:24

because normally because before this we

play03:26

could obviously not do this so we got

play03:28

badge here and all we have to do now is

play03:30

to update this number just like we

play03:32

update anything else in swift ui and it

play03:34

will update the badge automatically

play03:37

a really cool thing to point out here is

play03:39

that if the badge is zero it will just

play03:41

not display which is perfect because we

play03:42

don't want badges to pop up here if

play03:44

there's actually zero notifications

play03:47

but i do want to point out that

play03:48

generally we will use numbers here but

play03:50

we can also use strings so if we want

play03:52

the badge to say

play03:54

like new we could do something like that

play03:56

and we get that coming in

play03:58

the other place that we can use the

play04:00

badges of course and i'll comment this

play04:02

out is in a list let's put in a list

play04:05

choose text and let's just say hello

play04:07

world

play04:08

let's just paste a couple of these and

play04:11

we can call badge again with a count or

play04:14

a text usually on a list i think a text

play04:17

is more relevant now

play04:18

and we can say something like new items

play04:23

and the badge on the list looks a little

play04:25

different it's not a little red icon but

play04:27

it's some content on the right side that

play04:30

is in a secondary

play04:32

text color so our primary color is black

play04:35

here but our secondary badge here is

play04:37

light gray

play04:39

again we can use

play04:41

numbers here if we want

play04:43

and

play04:44

that's pretty much it

play04:46

and so now you guys know how to use

play04:47

badges which is pretty convenient when

play04:49

we're using lists or tab views

play04:51

thank you guys for watching as always

play04:53

i'm nick this is swiffle thinking and

play04:55

i'll see you in the next video

play04:57

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Swift UI徽章标签栏列表iOS 15教程编程XcodeUI设计应用开发技术更新
Do you need a summary in English?