How to add Badges to SwiftUI TabView and List in iOS 15 | Bootcamp #59
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
📱 Swift UI 应用中添加徽章
本段介绍了如何在 Swift UI 应用中添加徽章,这是 iOS 15 新增的功能。徽章可以用来在 Tab Bar 上显示通知数量,类似于 UIKit 中的长期功能。视频将展示如何在 Xcode 中创建一个简单的 Swift UI 视图,并在 Tab Bar 上添加徽章。
Mindmap
Keywords
💡Swift UI
💡徽章(Badges)
💡Tab Bar
💡List Rows
💡Xcode
💡系统名称(System Name)
💡Accent Color
💡Tab View
💡声明式语法
💡更新(Update)
💡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
[Music]
what's up everyone another quick video
coming at you guys today we're gonna
look at how we can add badges uh into
our swift ui apps now
badges probably should have been in the
first iteration of swift ui i don't know
why apple didn't include this the first
time but basically when we have a tab
bar and we want to put a little icon
indicating how maybe how many
notifications are on that tab we do that
using a batch we've had badges in ui kit
for a long time now but we did not have
them in swift ui until ios 15. so in
this video we're going to take a quick
look at how we can add a badge we're
also going to look at how we can add
badges or badges uh to lists so we can
add them to lists they look a little
different not like a typical badge it's
still called badge though
uh but still a nice feature to have so
let's jump into xcode and take a look
all right we are back in our xcode
project let's right click the navigator
create a new file it'll be a swift ui
view and let's call this one
badges bootcamp
let's click create
let's click resume on the canvas to make
sure we are connected here and
uh this one again will be a pretty
simple video i wish these had come out
when i first created this boot camp
because i would have included them in
the original videos of course
and there are two places that we can use
badges in swift ui if uh if we call that
badge here we can add an account it's
not going to do anything but if we right
click and jump to the definition of
badge
at least as of today when i'm making
this
we can see that badges are only
displayed in list rows and tab bars
so we can i guess add badge anywhere in
our code but it's really only going to
work on a list or a tab view
so let's write that down list and tab it
said tab bar but really we're gonna have
to use a tab view
and if you are just joining i did cover
tab view in its own series earlier in
its own video earlier in this series so
we have a whole video on tab view if you
don't know how to use it but let's
create a really simple one we'll just
create a a blank tab view here open the
brackets
inside i'm just going to put let's do
color.red
color.color.green
and color.blue these will be our three
tabs each tab is gonna have a tab
item
click enter tab item we'll have an image
we'll use a system name
let's do heart.fill and a text that says
let's just say hello for a second
and we can see the tabs coming in here
and i'm going to just copy and paste
this tab item on all three
and i want to point out again that the
color in the tab bar is the color of the
accent color in your project so my
project right now has this orange tint
if i go to the access if i go to the
assets folder that's because our accent
color is this orange
if i were to get rid of this color
come on back we'll get back to our
native default blue
uh but this video is all about the
badges so really simply we can add a dot
badge
and we're gonna go down to the count
option and let's just put in the number
five
and it's really that simple now we can
add badges to our tab bar which is so
convenient
because normally because before this we
could obviously not do this so we got
badge here and all we have to do now is
to update this number just like we
update anything else in swift ui and it
will update the badge automatically
a really cool thing to point out here is
that if the badge is zero it will just
not display which is perfect because we
don't want badges to pop up here if
there's actually zero notifications
but i do want to point out that
generally we will use numbers here but
we can also use strings so if we want
the badge to say
like new we could do something like that
and we get that coming in
the other place that we can use the
badges of course and i'll comment this
out is in a list let's put in a list
choose text and let's just say hello
world
let's just paste a couple of these and
we can call badge again with a count or
a text usually on a list i think a text
is more relevant now
and we can say something like new items
and the badge on the list looks a little
different it's not a little red icon but
it's some content on the right side that
is in a secondary
text color so our primary color is black
here but our secondary badge here is
light gray
again we can use
numbers here if we want
and
that's pretty much it
and so now you guys know how to use
badges which is pretty convenient when
we're using lists or tab views
thank you guys for watching as always
i'm nick this is swiffle thinking and
i'll see you in the next video
[Music]
Voir Plus de Vidéos Connexes
Create a List of Todo items in SwiftUI | Todo List #1
How to use SafeAreaInsets in SwiftUI | Bootcamp #65
Save and persist data with UserDefaults | Todo List #4
Create a custom data model for Todo items in SwiftUI | Todo List #2
User Experience and Animations in SwiftUI app | Todo List #5
macOS Menu Bar App (Code Along) | SwiftUI, Xcode
5.0 / 5 (0 votes)