📱 Mobile/iOS
[iOS - UIKit] Keyboard 내려가게 하는 방법 (return 키, 여백 눌렀을 때, 드래그 등)
kim_ghgh
2024. 5. 22. 17:08
return 키를 누르면 키보드를 내리는 방법
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == loginView.idTextField {
loginView.passwordTextField.becomeFirstResponder()
} else {
loginView.passwordTextField.resignFirstResponder()
}
return true
}
여백을 누르면 키보드를 내리는 방법
1번
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
2번
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboard()
}
func hideKeyboard() {
view.addGestureRecognizer(UITapGestureRecognizer(target: self,
action: #selector(UIViewController.dismissKeyboard)))
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
tableView를 사용할 경우 드래그해서 키보드 내리는 방법
tableView.keyboardDismissMode = .onDrag
728x90