SwiftUI-Button组件学习

SwiftUI 中,Button 组件用于创建一个可点击的按钮,它可以触发某个动作或事件。Button 是一种非常常用的 UI 组件,通常用来触发用户的交互,如提交数据、导航到新页面等。

基本用法
  • Button 组件的基本用法是绑定一个动作(action)和一个视图(label)。按钮的 action 会在用户点击按钮时执行。
less 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
}
按钮样式和修饰符

SwiftUI 提供了许多修饰符来定制按钮的外观和行为。你可以使用这些修饰符调整按钮的尺寸、背景色、形状、阴影、文字样式等。

1. 自定义文本样式

你可以通过 Text 组件设置按钮的标签文本样式。

scss 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
        .font(.title)
        .foregroundColor(.white)
        .padding()
        .background(Color.blue)
        .cornerRadius(10)
}
2. 添加阴影和圆角

你可以使用 shadowcornerRadius 等修饰符来增强按钮的视觉效果。

scss 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
        .font(.title)
        .padding()
        .background(Color.blue)
        .foregroundColor(.white)
        .cornerRadius(15)
        .shadow(radius: 10)  // 添加阴影
}
3. 改变按钮大小

可以通过 frame() 来设置按钮的固定大小。

css 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
        .frame(width: 200, height: 60)
        .background(Color.blue)
        .foregroundColor(.white)
        .cornerRadius(10)
}
4. 禁用按钮
  • disabled() 修饰符将按钮设置为禁用状态,按钮将无法响应任何交互。
scss 复制代码
@State private var isDisabled = false

Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
        .padding()
        .background(Color.blue)
        .foregroundColor(.white)
        .cornerRadius(10)
}
.disabled(isDisabled)  // 根据状态禁用按钮
5.按钮的角色

role 修饰符可以有以下几个常用值:

1.role(.button) - 普通按钮(默认值)
  • 这是默认的按钮类型,表示一个普通的按钮,没有特殊的语义含义。
css 复制代码
Button(action: {
    print("Normal button clicked!")
}) {
    Text("Normal Button")
}
2.role(.destructive) - 销毁性按钮
  • 这个角色表示一个具有破坏性的按钮,通常用于表示删除、销毁等操作。通常,具有这个角色的按钮会以不同的视觉效果呈现(如红色背景),并且在无障碍支持中,系统会更明确地标识其具有破坏性。
less 复制代码
Button(role: .destructive, action: {
    print("Delete action triggered!")
}) {
    Text("Delete Item")
        .foregroundColor(.red)  // 通常销毁性按钮会有红色文字
}
3.role(.cancel) - 取消按钮
  • 这个角色用于表示取消操作的按钮。它通常用于关闭对话框、弹出框或其他界面元素,表示用户想要放弃当前操作。
less 复制代码
Button(role: .cancel, action: {
    print("Cancel button clicked!")
}) {
    Text("Cancel")
        .foregroundColor(.blue)
}

role 的最大优势之一是帮助无障碍辅助技术(如 VoiceOver)理解按钮的语义。设置了 role 的按钮能够告诉辅助技术它的目的,使得视障用户能够更好地理解和使用你的应用。

按钮的交互状态

SwiftUI 中的按钮可以通过不同的状态来改变它的外观。例如,按钮在被按下时可能需要改变颜色或大小。你可以通过 ButtonStyle 或使用 @State 来管理按钮的状态。

1. 使用 @State 管理按钮的点击状态
scss 复制代码
@State private var isButtonPressed = false

Button(action: {
    isButtonPressed.toggle()
}) {
    Text(isButtonPressed ? "Pressed" : "Click Me")
        .padding()
        .background(isButtonPressed ? Color.green : Color.blue)
        .foregroundColor(.white)
        .cornerRadius(10)
}
2. 使用 ButtonStyle 自定义按钮的样式

你可以通过自定义 ButtonStyle 来实现按钮的不同交互效果。例如,改变按钮按下时的样式。

scss 复制代码
struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding()
            .background(configuration.isPressed ? Color.red : Color.blue)
            .foregroundColor(.white)
            .cornerRadius(10)
            .scaleEffect(configuration.isPressed ? 0.9 : 1)  // 按下时缩小
            .animation(.spring())
    }
}

Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
}
.buttonStyle(MyButtonStyle())  // 应用自定义样式
按钮的图标和文本

你也可以在按钮中组合图标和文本。通常,图标使用 Image(systemName:) 来加载系统图标。

scss 复制代码
Button(action: {
    print("Button clicked!")
}) {
    HStack {
        Image(systemName: "star.fill")
            .foregroundColor(.yellow)
        Text("Favorite")
            .font(.title)
    }
    .padding()
    .background(Color.blue)
    .foregroundColor(.white)
    .cornerRadius(10)
}
预定义按钮样式

SwiftUI 提供了几种预定义的按钮样式,你可以快速为按钮应用。

1. PlainButtonStyle
  • PlainButtonStyle 是默认的按钮样式,按钮在按下时不会改变样式。
less 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
}
.buttonStyle(PlainButtonStyle())  // 使用默认样式
2. BorderedButtonStyle
  • BorderedButtonStyle 提供了一个带有边框的按钮样式。
less 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Click Me")
}
.buttonStyle(BorderedButtonStyle())  // 带有边框的按钮样式
3. PlainButtonStyle + Hover Effect (macOS)
  • HoverEffect 适用于 macOSiPadOS,当用户将鼠标悬停在按钮上时,可以看到按钮的变化。
less 复制代码
Button(action: {
    print("Button clicked!")
}) {
    Text("Hover Over Me")
}
.buttonStyle(PlainButtonStyle())
.hoverEffect(.highlight)

SwiftUI Button 预定义按钮样式

样式名称 描述 使用示例
PlainButtonStyle 默认样式,按钮没有特殊的装饰或变化。适用于最基本的按钮需求。 swift\nButton("Click Me")\n .buttonStyle(PlainButtonStyle())\n
BorderedButtonStyle 带有边框的按钮样式,通常会有一个细的边框。适用于表单按钮等。 swift\nButton("Click Me")\n .buttonStyle(BorderedButtonStyle())\n
BorderlessButtonStyle 不带边框的按钮样式,适合一些特定的交互样式或无边框按钮。 swift\nButton("Click Me")\n .buttonStyle(BorderlessButtonStyle())\n
PlainButtonStyle() 没有自动的视觉变化或效果。常用于自定义外观的按钮。 swift\nButton("Click Me")\n .buttonStyle(PlainButtonStyle())\n
LinkButtonStyle 适用于Link组件的按钮样式,按钮看起来像是一个可点击的链接。 swift\nButton("Click Me")\n .buttonStyle(LinkButtonStyle())\n
DefaultButtonStyle 默认按钮样式(通常和PlainButtonStyle相同),适用于默认的交互。 swift\nButton("Click Me")\n .buttonStyle(DefaultButtonStyle())\n
相关推荐
Antonio9151 小时前
【Swift】 UIKit:UIGestureRecognizer和UIView Animation
开发语言·ios·swift
私人珍藏库4 小时前
利用 iPhone 或 Apple Watch ,自动锁定和解锁 Windows
ios·iphone
2501_9160088910 小时前
iOS 性能测试的深度实战方法 构建从底层指标到真实场景回放的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
kk哥889910 小时前
iOS 26 适配指南:UIScrollView 新特性与最佳实践
macos·ios·cocoa
iOS阿玮11 小时前
屁股坏了,我硬抗了3天,差不多省了几千块这件事儿。
ios
alloc16 小时前
Foundation Models Framework
ios
ajassi200020 小时前
开源 Objective-C IOS 应用开发(二十三).a静态库的封装和使用
ios·开源·objective-c
明远湖之鱼21 小时前
浅入理解流式SSR的性能收益与工作原理
前端·ios
白玉cfc21 小时前
【iOS】多线程基础
macos·ios
2501_915909061 天前
iOS APP 抓包全流程解析,HTTPS 调试、网络协议分析与多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview