アプリ開発で「ボタン」はユーザーの操作を受け取る重要なUIパーツです。
SwiftUIでは、シンプルで直感的なコードで Button を実装できます。
1.一番基本的なボタンの書き方
import SwiftUI
struct ContentView: View {
var body: some View {
Button("押す") {
print("ボタンが押されました")
}
}
}
2.ボタンの見た目をカスタマイズする
Button(action: {
print("カスタムボタン")
}) {
Text("カスタムボタン")
.padding()
.background(.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
3.アイコン付きボタン(Label を使う)
Button(action: {
print("アイコン付きボタン")
}) {
Label("保存する", systemImage: "square.and.arrow.down")
}
まとめ
機能 | 実装方法の例 |
---|---|
基本のボタン | Button("タイトル") { 処理 } |
デザインカスタム |
Text().padding().background() など |
アイコン付き | Label("タイトル", systemImage: "アイコン") |
最後に
SwiftUIのButtonは、基本をマスターすればどんどん応用が効くようになります!
Swift UI のButtons基礎の使い方を説明しました!ぜひ参考にしてみてください👍