iOS/코드조각

[iOS, Swift] ScrollView 키보드 화면 처리하기

검은참깨두유vm 2022. 7. 12. 19:15
반응형

iOS 15.5, Xcode 13.31, Swift 5, UIKit 환경에서 진행했습니다.

 

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowInScroll), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideInScroll), name: UIResponder.keyboardWillHideNotification, object: nil)

Notification 등록

 

@objc func keyboardWillShowInScroll(_ notification: Notification) {
    guard let userInfo = notification.userInfo,
          let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
        return
    }
    let contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
    scrollView.contentInset = contentInset
    scrollView.scrollIndicatorInsets = contentInset
}

키보드 화면 처리

 

반응형