티스토리 뷰
[iOS] Project - BuyOrNot
[iOS] TableView + TextField 포함되어 있는 View에서 Keyboard 사용시 Layout 이슈
ungQ 2024. 4. 25. 23:30- 이번 프로젝트에서는 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
'[iOS] Project - BuyOrNot' 카테고리의 다른 글
[iOS] propertyWrapper를 활용한 UserDefaults 캡슐화 (0) | 2024.04.28 |
---|---|
[iOS] API 통신시, response값이 없을 경우 (0) | 2024.04.26 |
[iOS] 제네릭 활용, Network 메서드 통합 작업 (0) | 2024.04.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 타입불일치
- 한국관광공사
- 라우터패턴
- SnapKit
- ios
- Xcode
- Alamofire
- Generic
- Swift
- accessibilityidentifier
- optimisticui
- remakeconstraints
- network
- RxSwift
- urlsession
- UserDefaults
- makeconstraints
- 빈배열
- tabman
- API
- DiffableDataSource
- layout
- PropertyWrapper
- SwiftUI
- Router
- custommodifier
- Concurrency
- TableView
- collectionView
- routerpattern
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함