1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUIのbackgroundとoverlayの使い方を簡単に解説してみました!

Posted at

SwiftUIでビューの見た目をカスタマイズする際に便利なのが、backgroundとoverlayです。

そもそもbackgroundとoverlayとは?

修飾子名 意味
background 背景に色や画像・図形を「後ろに」表示する
overlay 上にテキストや枠線などを「前に」重ねる

backgroundの基本的な使い方

Text("Hello!")
    .padding()
    .background(.yellow)

Screenshot 2025-06-05 at 15.17.10.png

解説:
テキストの背景に黄色が付きます。

padding()でスペースを確保し、その外側にbackgroundが付きます。

overlayの基本的な使い方

Text("Hello, SwiftUI!")
    .padding()
    .overlay(
        RoundedRectangle(cornerRadius: 10)
            .stroke(Color.blue, lineWidth: 2)
    )
  • テキストの上に青い枠線が付きます。
  • overlayには任意のViewを載せることが可能です(今回は枠線用のRoundedRectangleを使用)
    Screenshot 2025-06-05 at 15.18.13.png

backgroundとoverlayの組み合わせを組み合わせると

Text("SwiftUI")
    .font(.title)
    .padding()
    .background(.black)
    .overlay(
        RoundedRectangle(cornerRadius: 12)
            .stroke(Color.orange, lineWidth: 3)
    )
    .foregroundColor(.white)

この例では:

  • 背景は黒(background)
  • 外枠はオレンジの線(overlay)
    Screenshot 2025-06-05 at 15.19.33.png

まとめ

修飾子 主な用途 使い方例
background 背景色・画像・形を後ろに配置 .background(Color.red)
overlay 枠線・テキスト・ラベルを前に重ねる .overlay(RoundedRectangle(...))

終わりに

backgroundとoverlayを活用することで、SwiftUIの見た目を自由にデザインできます。
ぜひ参考にしてみてください!👏

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?