티스토리 뷰

  • 이번 프로젝트에서는 IQKeyboard 라이브러리를 적용하여 회원가입, 로그인 뷰 등에서 키보드 관련 Layout 이슈는 없었다고 생각하였습니다.
  • 그치만 역시 세상에 쉬운 일은 없는법... Simulator에서 대부분 키보드를 Hide하여 테스트하다보니 이슈를 뒤늦게 발견하였습니다..
  • 댓글View를 상단에 TableView, 하단에 TextField 를 배치하였는데 TextField 클릭시 IQKeyboard 특성상 TableView의 전체가 모두 올라가버리는 이슈를 마주하였씁니다. 현상은 아래의 이미지와 같습니다!

Issue Preview

접근한 해결 방법

  • 우선 해당 뷰에서는 IQKeyboard 기능을 Off
  • IQKeyboardManager.shared.disabledDistanceHandlingClasses = [CommentViewController.self]
  • 그 후 textField의 bottom 과, button의 bottom 을 동적인 Layout 업데이트
    private var textFieldBottomConstraint: Constraint?
    private var buttonBottomConstraint: Constraint?

        func registerKeyboardNotification()
    {
        // 키보드 표시 노티피케이션 등록
        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) {
        if let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardFrame.height - view.safeAreaInsets.bottom

            UIView.animate(withDuration: 0.3) {
                self.textFieldBottomConstraint?.update(inset: keyboardHeight + 10)
                self.buttonBottomConstraint?.update(inset: keyboardHeight + 10)
                self.commentTableView.snp.updateConstraints { make in
                    make.bottom.equalTo(self.commentTextField.snp.top).offset(-10)
                }
                self.view.layoutIfNeeded()
            }
        }
    }

    @objc func keyboardWillHide(_ notification: Notification) {
        UIView.animate(withDuration: 0.3) {
            self.textFieldBottomConstraint?.update(inset: 10)
            self.buttonBottomConstraint?.update(inset: 10)
            self.commentTableView.snp.updateConstraints { make in
                make.bottom.equalTo(self.commentTextField.snp.top).offset(-10)
            }
            self.view.layoutIfNeeded()
        }
    }

Solved Preview

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함