updated constraint could not find existing matching constraint to update 해결하기

Daehee Kim·2021년 12월 8일
1

문제상황

snapkit으로 채팅창 TextField UI를 만들고 있었는데 키보드가 올라올 때에 이 애러가 생긴다.

https://github.com/SnapKit/SnapKit/issues/295
https://stackoverflow.com/questions/43462122/update-constraint-does-not-work-on-uitableview-snapkit

뭐 이런 좋은 답변은 많았지만 나는 적용되지 않았다.

그러다가 우연히 신기한 거를 발견했는데..

        inputTextFieldView.snp.makeConstraints {
            $0.width.equalTo(self.view.frame.width)
            $0.height.equalTo(60)
            $0.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
            $0.centerX.equalTo(self.view).offset(0)
        }

요거는 키보드 터치 안했을 때 코드였고

    @objc func keyboardWillShow(noti: Notification) {
        let notinfo = noti.userInfo!
        let keyboardFrame = notinfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
        let heiget = keyboardFrame.size.height - self.view.safeAreaInsets.bottom
        let animateDuration = notinfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
        UIView.animate(withDuration: animateDuration) {
            self.inputTextFieldView.snp.updateConstraints() {
                $0.bottom.equalTo(-heiget)
            }
            self.view.layoutIfNeeded()
        }
    }

키보드가 올라갔을 때 실행되는 함수이다.

저 safeAreaLayoutGuide를 사용하면 updateConstraints를 할 때에 애러가 난다.

        inputTextFieldView.snp.makeConstraints {
            $0.width.equalTo(self.view.frame.width)
            $0.height.equalTo(60)
            $0.bottom.equalTo(0)
            $0.centerX.equalTo(self.view).offset(0)
        }

이렇게 해 주면 애러가 발생하지 않는다.
혹여나 safeArea를 사용하려면 뒤에 safeArea용 View를 하나 생성하는 거도 방법이 될 수 있다.

SnapKit 공부를 좀 해야될 거 같다. Rx는 언제 해야되나....

profile
IOS 공부중입니다

0개의 댓글