0
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のFrameとAlignmentの使い方まとめ

Posted at

SwiftUIでアプリを作っていると、「文字や画像を指定のサイズにしたい」「端に寄せたい」といった場面がたくさん出てきます。
そんな時に使うのが frame と alignment!

この記事では、この2つの使い方を、初心者にも分かりやすく解説します! 🙌

frameとは?

ビューの幅や高さを指定するための修飾子です。

Text("Hello, SwiftUI!")
    .frame(width: 200, height: 400) //幅:200ポイント、高さ:400ポイントの枠を作る
    .background(.yellow)

例:
Screenshot 2025-05-31 at 15.54.47.png

背景色をつけると、サイズ感が分かりやすい!

alignmentとは?

使い方の例(VStack, HStack, ZStackなどで使用):

VStack(alignment: .leading) {
    Text("左寄せのテキスト")
}
.frame(width: 200)
.background(.blue)

主なalignmentの種類:

使い方 意味
.leading 左寄せ(横)
.trailing 右寄せ(横)
.center(デフォルト) 中央寄せ
.top / .bottom 上下の寄せ(主にZStackやVStackで使用)

frame + alignment の組み合わせ

Text("右下に表示")
    .frame(width: 200, height: 100, alignment: .bottomTrailing)
    .background(Color.gray.opacity(0.3))

例:
Screenshot 2025-05-31 at 16.09.02.png

まとめ

機能 目的
frame 幅・高さの枠を決める .frame(width: 200, height: 100)
alignment 枠内やスタック内での位置を決める .frame(alignment: .topLeading)

おわりに

SwiftUIのframealignmentを理解すると、レイアウトの自由度が一気に広がります!
frameAlignmentはよく使うのでぜひマスターしてください!✌️

0
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
0
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?