• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    미해결

Chat_6 키보드 올라갈때 테이블 스크롤 이동 이슈

20.10.16 22:13 작성 조회수 167

0

어느정도 채팅을 치고 키보드를 내렸다 다시 올릴경우

그땐 어떻게 테이블 스크롤이 마지막 채팅을 향하게 하나요??

답변 1

답변을 작성해보세요.

0

chungchung님의 프로필

chungchung

2021.04.18

keyboardWillShow에서 animation이 끝난 시점에 lastIndexPath로 테이블뷰를 스크롤 해주는 방법이 있을 것 같아요.

animation의 완료와 상관없이 테이블뷰를 스크롤하면, 아직 테이블뷰의 bottom이 저 아래에 있어서 제대로 안되는 것 같네요.

  @objc private func keyboardWillShow(noti: Notification) {
    let notiInfo = noti.userInfo!
    let keyboardFrame = notiInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
    let height = keyboardFrame.size.height - self.view.safeAreaInsets.bottom
    let animationDuration = notiInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
    
    inputTextViewBottomMargin.constant = height
    
    UIView.animate(withDuration: animationDuration) {
      self.view.layoutIfNeeded()
    } completion: { [unowned self] isCompleted in
      if isCompleted {
        if !self.chatDatas.isEmpty {
          let lastIndexPath = IndexPath.init(row: self.chatDatas.count - 1, section: 0)
          self.chatTableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
        }
      }
    }
  }