아주 간단하다 텍스트 필드 인스턴스에다가 endEditing 메소드를 호출하면 된다.
import UIKit
class WeatherViewController: UIViewController {
@IBOutlet weak var searchTextField: UITextField!
@IBAction func searchPressed(_ sender: UIButton) {
searchTextField.endEditing(true)
}
}
키보드의 return 버튼을 눌렀을때 사라지게 하고 싶으면
1. 해당 뷰 컨트롤러 클래스에서 UITextFieldDelegate 채택
2. viewDidLoad 에서 searchTextField.delegate = self
3. UITextFieldDelegate 프로토콜의 함수 textFieldShouldReturn에다가 endEditiong 메소드 호출
import UIKit
class WeatherViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var searchTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.delegate = self
}
@IBAction func searchPressed(_ sender: UIButton) {
searchTextField.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchTextField.endEditing(true)
return true
}
}
반응형
'swift & iOS > ios & xcode' 카테고리의 다른 글
[ios 개발] xcode에서 code snippet 코드 스니펫 만들기 (0) | 2022.02.08 |
---|---|
[ios 개발] UITextFieldDelegate의 유용한 메소드 3가지 (0) | 2022.01.31 |
[xcode] 버튼 모서리를 둥글게 하기 (0) | 2022.01.26 |
[xcode] 텍스트 필드에 숫자 키보드 띄우기 (keyboard type) (0) | 2022.01.26 |
[xcode] library 단축키 (0) | 2022.01.24 |