「Swift」Buttonビューのボタンに画像を使うサンプル

書式
Button(action: {
// ボタンがタップされた時の処理コード
}) {
// ボタンのView
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
var body: some View {
   //ボタンタップ
Button(action: {
// ボタン押された時の処理
print("登録ボタンがタップされました")
}) {
// アイコン
Image(systemName: "plus")
// テキスト
Text("登録").font(.title)
}
.foregroundColor(Color.white)
.padding(15)
.background(Color.skyblue)
}
}
struct ContentView: View { var body: some View {    //ボタンタップ Button(action: { // ボタン押された時の処理 print("登録ボタンがタップされました") }) { // アイコン Image(systemName: "plus") // テキスト Text("登録").font(.title) } .foregroundColor(Color.white) .padding(15) .background(Color.skyblue) } }
struct ContentView: View {

    var body: some View {
         //ボタンタップ
        Button(action: {
            // ボタン押された時の処理
            print("登録ボタンがタップされました")
        }) {
            // アイコン
            Image(systemName: "plus")
                  // テキスト
            Text("登録").font(.title)
        }
        .foregroundColor(Color.white)
        .padding(15)
        .background(Color.skyblue)
    }
}

結果
初期画面にテキスト「登録」とアイコン(+)が表示されます。
「登録」ボタンをタップすると、print()を実行してメッセージを出力します。

Swift

Posted by arkgame