When typing TextFeild in Swift, it is common for the keyboard coming out of the screen to be invisible.
I searched and found that all of them were implemented by myself.
But I found a useful library.
Write in pod file and execute pod install
pod 'TPKeyboardAvoiding'
$ pod install
Analyzing dependencies
Downloading dependencies
Installing TPKeyboardAvoiding (1.3.3)
Generating Pods project
Integrating client project
Place scroll view
Place a view under that
The structure is like this.
Keyboard…is ScrollView
SecondView is View
Constraints from default View to ScrollView are
Set top, bottom, left and right to 0
Constraints from ScrollView to View are
Set Align Center X, Y
Basically, this library picks up the input start event of TextField,
Only the processing when the return key is pressed is described.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var yourNameTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UITextFieldDelegate{
//Close text field with return key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}