组件Components
ButtonButton
用于触发操作的点击控件,支持文本按钮、自定义 Label、样式定制和事件绑定。A control for triggering actions, with text labels, custom labels, styles, and event handlers.
入门Beginner • 4 分钟4 min
更新于Updated Apr 22, 2026
概览Overview
Button 是 SwiftUI 中最基础的交互组件之一。它可以只接收文本标题,也可以通过 Label 或自定义视图构建更丰富的点击目标。Button is one of SwiftUI’s most basic interactive controls. It can use a text title, a Label, or a custom view as its tap target.
什么时候用 ButtonWhen to use Button
当用户需要主动触发保存、继续、删除、确认等行为时,优先使用 Button。Use Button when the user needs to actively save, continue, delete, or confirm something.
Basic ExampleBasic Example
基础代码示例Basic Code Example
swift
1Button("Save") {2 print("Tap")3}4.buttonStyle(.borderedProminent)5.tint(.blue)
控制台
点击 Save 查看 print 输出。
参数Parameters
常见参数Common Parameters
titletitle
按钮文字Button title
actionaction
点击事件Tap action
rolerole
按钮语义Button role
labellabel
自定义内容Custom content
常见搭配Common Modifiers
buttonStyletintcontrolSizedisabledlabelStyle
易错点Pitfalls
易错点Pitfalls
多个样式 modifier 的顺序会影响视觉结果;destructive 角色只应用于危险操作;复杂按钮推荐使用 Button(action:) 搭配自定义 Label。Modifier order can affect the final appearance; use destructive only for risky actions; complex buttons are often clearer with Button(action:) and a custom Label.