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?

More than 1 year has passed since last update.

【SwiftUI】SectionのHeaderとContentのspacingを調整する

Last updated at Posted at 2023-12-21

はじめに

こんにちは。ymurao2です!
Viewのグループ化をSectionを利用して行う際、HeaderとContentのスペースの調整方法が分からなかったので、記事に残しておきます

想定する画面

想定する画面は以下のようなものです。
Contentの上にHeaderを表示しています。

CleanShot 2023-12-21 at 21.38.18.png

実装

var body: some View {
+   VStack(spacing: 0) {
        Section {
            Text("Content")
                .font(.title)
                .frame(maxWidth: .infinity)
                .frame(height: 300)
                .background(Color.gray.opacity(0.2))
        } header: {
            Text("Header")
                .font(.caption)
                .frame(maxWidth: .infinity, alignment: .leading)
                .background(Color.gray.opacity(0.4))
        }
+  }
}

SectionVStackで囲んで、spacingを付与することでContentとHeaderのスペースを調整できることがわかりました。
0を指定することで、ContentとHeaderがピッタリくっついています。

var body: some View {
    VStack(spacing: 8) {
        Section {
            // 省略
        } header: {
            // 省略
    }
}

spacingに8を指定したときは以下のとおりです

CleanShot 2023-12-21 at 21.40.32.png

おわりに

当然と言えば当然かもしれませんが、簡単に実装できました
(もっと早く知りたかった...)

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?