方法一:使用background修饰符 你可以使用background修饰符来设置按钮的背景色。以下是一个示例: 代码语言:txt 复制 import SwiftUI struct ContentView: View { var body: some View { Button(action: { print("Button tapped!") }) { Text("Click Me") .foregroundColor(.white) .font(.headline) } .back...
@State private var buttonColor = Color.blue 然后,在按钮的background修饰符中使用buttonColor来设置按钮的背景颜色: 代码语言:txt 复制 Button(action: { // 在按钮点击时改变按钮的颜色 buttonColor = Color.red }) { Text("按钮") .foregroundColor(.white) } .background(buttonColor) 在按钮的a...
.button1 {background-color: #4CAF50;} /* 绿色 */ .button2 {background-color: #008CBA;} /* 蓝色 */ .button3 {background-color: #f44336;} /* 红色 */ .button4 {background-color: #e7e7e7; color: black;} /* 灰色 */ .button5 {background-color: #555555;} /* 黑色 */ .b...
1. 默认样式 // 默认是automaticButton("默认"){}Button("automatic"){}.buttonStyle(.automatic).foregroundColor(Color.mint)Button("bordered"){}.buttonStyle(.bordered).foregroundColor(Color.mint)Button("borderedProminent"){}.buttonStyle(.borderedProminent) 2. 自定义文本按钮 自定义label,可实现各种想要...
Button(action: { // 操作 print("登录成功") }) { // 按钮样式 Text("微信登录") .font(.system(size:14)) .frame(minWidth:0,maxWidth:.infinity) .padding() .foregroundColor(.white) .background(Color(red:88/255,green:224/255,blue:133/255)) ...
效果就如上一个按钮,点击实现星和圆的颜色变化。 代码如下: structContentView:View{@StateprivatevarstarColor:Color=.redvarbody:someView{VStack{Button(action:{print("点击了按钮\(arc4random() % 255)")self.starColor=Color(red:Double(arc4random()%255)/255.0,green:Double(arc4random()%255)/255.0...
Button("Click Me") { // 点击事件 } 您可以像这样给按钮添加属性: Button(action: { }, label: { Image(systemName: "clock") Text("Click Me") Text("Subtitle") }) .foregroundColor(Color.white) .padding() .background(Color.blue) .cornerRadius(5) 为了更好的运用 Button,下面我们将一起来...
// 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)...
第一步,创建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...