[SwiftUI]Button(ボタン)の背景をグラデーションにするには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

Buttonの背景をグラデーションにする方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Buttonの背景をグラデーションにするには、background修飾子を使います。

まず、Buttonにbackground修飾子を付与します。

そして、background修飾子の引数にグラデーションを指定します。

Button(・・・)
   .background(LinearGradient(・・・))

引数にグラデーションを指定したbackground修飾子をButtonに付与することで、Buttonの背景をグラデーションにすることが出来ます。

グラデーションには、次のようなものがあります。

  • LinearGradient : 直線状のグラデーション
  • RadialGradient : 放射状のグラデーション
  • AngularGradient:円錐状グラデーション

使用例

struct ContentView: View {
    var body: some View {
        VStack {
            Button(action: {}) {
                Text("Button")
                    .foregroundColor(.white)
                    .padding(20)
            }
            .background(
                LinearGradient(
                    colors: [.red, .yellow],
                    startPoint: .leading,
                    endPoint: .trailing
                )
            )
        }
    }
}

コメント

タイトルとURLをコピーしました