나의 기록, 현진록

[iOS] 키보드 높이에 따라 동적 높이 조절 UIKeyBoardLayoutGuide 본문

카테고리 없음

[iOS] 키보드 높이에 따라 동적 높이 조절 UIKeyBoardLayoutGuide

guswlsdk 2023. 6. 7. 16:17
반응형

 

 

UIKeyboardLayoutGuide | Apple Developer Documentation

A layout guide that represents the space the keyboard occupies in your app’s layout.

developer.apple.com

 

기존에는 키보드가 사용되는 순간 뷰를 가리는 문제를 해결하기 위해서는 Notification을 등록하고, 키보드의 높이를 구하고, 높이만큼 뷰 높이를 수정하는 복잡한 과정을 이용했다면 iOS 15부터는 UIKeyBoardLayoutGuide를 사용해서 간단하게 AutoLayout을 작성할 수 있다.

 

 

 

다음은 문제되는 상황을 시뮬레이션한 것이다.

키보드가 올라오면 UITextField를 가리는 문제가 발생한다.

 

scrollView.snp.makeConstraints { make in
        make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
        make.trailing.leading.equalToSuperview()
	make.bottom.equalToSuperview()
}

 

 

 

 

다음은 UIKeyBoardLayoutGuide를 사용하여 UITextField의 Autolayout을 수정한 모습이다.

 

scrollView.snp.makeConstraints { make in
        make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
        make.trailing.leading.equalToSuperview()
	make.bottom.equalTo(self.view.keyboardLayoutGuide.snp.top)
}

 

반응형