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?

More than 1 year has passed since last update.

WKWebViewでキーボードを展開中に画面がスクロールされたらキーボードを閉じる

Last updated at Posted at 2023-06-08

タイトルママ

設定方法

  • UIScrollViewDelegateを定義
final class WebViewModel: NSObject, ObservableObject, WKNavigationDelegate, WKUIDelegate, UIScrollViewDelegate {
  • delegate登録
webView.scrollView.delegate = self
  • webviewにkeyboardDismissModeを設定
webView.scrollView.keyboardDismissMode = .onDrag // 画面ドラッグしたとき、キーボードを閉じる

おまけ:キーボードの表示/非表示の監視

  • キーボードの表示/非表示を監視
// キーボード表示通知を登録
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)

// キーボード非表示通知を登録
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  • キーボード表示/非表示されたときの処理
    // キーボードが表示されるときの処理
    @objc func keyboardWillShow(_ notification: Notification) {
        print("Keyboard will show")
    }
    
    // キーボードが非表示になるときの処理
    @objc func keyboardWillHide(_ notification: Notification) {
        print("Keyboard will hide")
    }
    
    deinit {
        // NotificationCenterの登録解除
        NotificationCenter.default.removeObserver(self)
    }
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?