LoginSignup
0
2

More than 1 year has passed since last update.

【Swift UI】keyboard表示時の自動スクロール制御

Last updated at Posted at 2022-08-06

iOS14からフォーカスが当てられたテキストフィールドが
キーボード表示領域の真上あたりに表示されている。

対策

swift
struct ContentView: View {
    @State var text = ""
    @State var workLift = true
    var body: some View {
        GeometryReader { _ in
            VStack(alignment: .leading) {
                Spacer()
                Text("Not Work Auto Lift Keyboard TextField")
                TextField("placeholder", text: self.$text)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button("Work Auto Lift Keyboard") {
                    self.workLift.toggle()
                }
                Spacer()
            }
       }
       .padding()
       .ignoresSafeArea(.keyboard, edges: .bottom)
    }
}

主に対策しているのはこれ
.ignoresSafeArea(.keyboard, edges: .bottom)

対策するには上記設定とView間の空白を埋めること
空白を埋めるには、Spacer()の他にVStackとか
今開発中のアプリでは以下を適用した

swift
struct ContentView: View {
    var body: some View {
        // ScrollViewの適用
        ScrollView(.vertical, showsIndicators: false) {
            VStack(spacing: 0) {
            }
        }
    }
}

またはスクロールビューにスクロールをさせないようにするには第一引数[]を入れる

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