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?

More than 3 years have passed since last update.

【SwiftUI】TextField&TextEditorのキーボード出現時のViewの動きを制限

Posted at

何をするのか?

SwiftUIではTextFieldやTextEditorをタップした際に表示されるキーボードにViewが隠れないように、自動でViewのレイアウトを調整してくれます。ですがモーダル(フルスクリーンじゃない)を表示した場合などにレイアウトの調整が行われると、表示しておきたいViewが画面外に追いやられてしまうことがあります。
そのため今回はあえて自動のレイアウト調整を無効にする方法を備忘録としてまとめておきます。

解決策

キーボードは SafeArea を基準にビューを伸縮させています。iOS 14 から追加された View.ignoresSafeArea() を利用してキーボードの表示に対し任意のビューを SafeArea から除外できるようになりました。
どのsafeAreaを指定するかは状況に応じて使い分けてください。

var body: some View {
    VStack {
        ...
    }
    .ignoresSafeArea(.keyboard, edges: .all)
}

参考

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?