Swift 画像をButtonで使うサンプル
環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
Ubuntu 20.04.2 LTS
構文
Button(action: {
//タップの処理コード
},label: {
Image("xxxx")
})
Button(action: {
//タップの処理コード
},label: {
Image("xxxx")
})
Button(action: { //タップの処理コード },label: { Image("xxxx") })
Buttonの引数「action」にクロージャーを指定します。
引数「label」にクロージャーを指定し、クロージャ内に画像としてImageを指定します。
使用例
struct TestView: View {
var body: some View {
VStack{
Button(action: {
print("pressed")
},label: {
Image("dogs")
})
}
}
}
struct TestView: View {
var body: some View {
VStack{
Button(action: {
print("pressed")
},label: {
Image("dogs")
})
}
}
}
struct TestView: View { var body: some View { VStack{ Button(action: { print("pressed") },label: { Image("dogs") }) } } }