方法一:使用background修饰符 你可以使用background修饰符来设置按钮的背景色。以下是一个示例: 代码语言:txt 复制 import SwiftUI struct ContentView: View { var body: some View { Button(action: { print("Button tapped!") }) { Text("Click Me") .f
我们使用.background()修饰符来实现这一点,并将按钮的形状调整为圆角矩形。 importSwiftUIstructContentView:View{varbody:someView{Button(action:{print("按钮被点击了")}){Text("点击我").font(.title).padding().foregroundColor(.white).background(Color.blue)// 设置背景颜色.cornerRadius(10)// 设置圆角...
Button(action:{// Button action here}){Text("点击我")} 1. 2. 3. 4. 5. 当我们未使用.background()时,该按钮的背景色为系统默认风格(如蓝色)。理想情况下,我们希望为按钮自定义背景色,也可以添加圆角等效果。 类图(配置项关联) uses >Button+label: Text+action:() : -> VoidBackground+color: ...
struct ClearButtonStyle: ButtonStyle { func makeBody(configuration: Self.Configuration) -> some View { configuration.label .padding(10) .background(Color.clear) .foregroundColor(.blue) .cornerRadius(10) .overlay( RoundedRectangle(cornerRadius: 10) .stroke(Color.blue, lineWidth: 2) ) .scaleEffect(...
Button(action: { self.change.toggle() }, label: { Text("Hello World") .padding() .background(Color.purple) .foregroundColor(.white) .font(.title) }).padding() 特别强调:modifier 的顺序非常重要,假设我们把 padding 移动,不在 background 的前面,效果是不一样的。 Button(action: { self.cha...
SwiftUI 控件之Button 设置图片按钮 Button(action: { print("Delete tapped!") }) { HStack { Image(systemName:"trash") .font(.title) Text("Delete") .fontWeight(.semibold) .font(.title) } .padding() .foregroundColor(.white) .background(Color.red) ...
第一步,创建Button代码,并在样式部分使用Image。 //图标按钮 struct SwiftUIIconView: View { var body: some View { Button(action: { // 操作 }) { // 按钮样式 Image("weixin") .resizable() .frame(minWidth: 0, maxWidth: 32, minHeight: 0, maxHeight: 32) .padding() .background(Color(red...
// 1、字符串构建,支持本地化字符串Button("Hello World") {// action} // 2、自定义样式Button{// action} label: {VStack(spacing:8) {Image(systemName:"square.and.arrow.up")Text("Share") } .padding() .foregroundColor(.white) .background(Color.blue) ...
Button(action: { print("被点击了呃") }){ Text("Hi~这是另一个 Button") } .font(.title) .padding() .frame(minWidth: 0, maxWidth: .infinity) .background(LinearGradient(gradient: Gradient(colors: [Color("LightGreen"), Color("DarkGreen")]), startPoint: .leading, endPoint: .trailing)...
struct ContentView: View {var body: some View {Button(action: {// 操作print("登录成功")}) {// 按钮样式Text("微信登录").font(.system(size: 14)).frame(minWidth: 0, maxWidth: .infinity).padding().foregroundColor(.white).background(LinearGradient(gradient: Gradient(colors: [Color.blue,...