ButtonStyle 与 LabelStyle
构建具有品牌风格且可复用的控件样式。
ButtonStyle 与 LabelStyle 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Style Controls, Not Instances
Restyling every button by hand gets tedious. A ButtonStyle defines one branded look you reuse across the whole app. 🎨
The ButtonStyle Protocol
Conform a struct to ButtonStyle and implement makeBody. It receives a configuration and returns the styled button.
struct PrimaryStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
}
}The label Inside Configuration
The configuration gives you the buttons label, which is the title or content. You decorate that label with padding and color.
configuration.label
.padding()
.background(.blue)React to isPressed
Configuration also exposes isPressed. Use it to dim or scale the button while a finger is down for tactile feedback.
configuration.label
.opacity(configuration.isPressed ? 0.6 : 1)Apply with buttonStyle
Attach your style with the .buttonStyle modifier. Every button it touches adopts the same branded appearance.
Button("Save") { }
.buttonStyle(PrimaryStyle())Style a Whole Container
Set .buttonStyle on a stack and every button inside inherits it. One line styles a full group of controls.
Built-in Styles Exist Too
SwiftUI ships ready styles like .bordered and .borderedProminent. Reach for those before writing a custom one.
Button("Go") { }
.buttonStyle(.borderedProminent)Now Meet LabelStyle
A Label pairs text with an icon. A LabelStyle controls how that pair is arranged and shown.
Label("Home", systemImage: "house")Title-Only or Icon-Only
Built-in label styles let you show just the title, just the icon, or both. Switch based on space without changing the call site.
Label("Home", systemImage: "house")
.labelStyle(.iconOnly)Custom LabelStyle
Conform to LabelStyle and implement makeBody to rearrange the icon and title yourself, like stacking them vertically.
func makeBody(configuration: Configuration) -> some View {
VStack { configuration.icon; configuration.title }
}One Definition, Many Buttons
Custom styles keep look and behavior in one place. Tweak the style and every button or label across the app updates together.
Quick Check
Quick check on control styles.
Recap: Reusable Control Looks
Use ButtonStyle and LabelStyle to define branded controls once. React to isPressed, rearrange labels, and apply the look everywhere. 🧠
常见问题解答
「ButtonStyle 与 LabelStyle」课时是免费的吗?
是的 — 「ButtonStyle 与 LabelStyle」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「ButtonStyle 与 LabelStyle」这节课中我会学到什么?
构建具有品牌风格且可复用的控件样式。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「ButtonStyle 与 LabelStyle」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 提取可复用视图
- 自定义 ViewModifiers
- ButtonStyle 与 LabelStyle
- ViewBuilder 与泛型容器