728x90
반응형
UITextField를 담고 있는 UIScrollView를 만든다.
scrollview contain textfield.
textField의 키보드가 나타날때 textfield가 가려지지 않도록 textfield를 스크롤 되게한다.
when textfield's keyboard show up, scroll up to textfield for textfield not covered.
class MainViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textFeid: UITextField!
overrid func viewDidLoad() {
super.viewDidLoad()
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 private func keyboardWillShow(_ notification: Notification)
{
print("keyboardWillShow")
guard let userInfo = notification.userInfo,
let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
return
}
scrollView.contentInset.bottom = keyboardFrame.size.height
scrollView.scrollRectToVisible(textField.frame, animated: true)
}
@objc private func keyboardWillHide(_ notification: Notification)
{
print("keyboardWillHide")
let contentInset = UIEdgeInsets.zero
scrollView.contentInset = contentInset
scrollView.scrollIndicatorInsets = contentInset
}
}
728x90
반응형
'개발 > ios (swift)' 카테고리의 다른 글
UITextview Placeholder (0) | 2022.03.03 |
---|---|
action when clicked button in cell of collectionView (0) | 2022.03.02 |
ios version에 따라 gallery에서 이미지 가지고 오는 방법 (0) | 2022.03.02 |
사파리로 링크 열기 (0) | 2022.02.18 |
댓글