1
2

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】safeAreaPaddingでScrollViewの開始位置と終了位置を調整する

Posted at

はじめに

ScrollViewpaddingを付けるとScrollViewが小さくなってしまいます。
しかし、safeAreaPaddingだとScrollViewは小さくならず、コンテンツのみにスペースが入っているっぽいです。

比較

paddingだとScrollViewが小さくなって途中で途切れているのがわかります。

padding safeAreaPadding
Simulator Screen Recording - iPhone 15 - 2024-01-22 at 21.59.53.gif Simulator Screen Recording - iPhone 15 - 2024-01-22 at 22.00.31.gif

paddingでの実装

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                ForEach(0..<10) { _ in
                    RoundedRectangle(cornerRadius: 10)
                        .foregroundStyle(.cyan)
                        .frame(height: 60)
                        .containerRelativeFrame(.horizontal)
                }
            }
        }
        .padding(.horizontal, 20)
    }
}

safeAreaPaddingでの実装

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                ForEach(0..<10) { _ in
                    RoundedRectangle(cornerRadius: 10)
                        .foregroundStyle(.cyan)
                        .frame(height: 60)
                        .containerRelativeFrame(.horizontal)
                }
            }
        }
        .safeAreaPadding(.horizontal, 20)
    }
}

公式ドキュメント

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?