Reputation: 336
I have an app in swift, where the texfield is located at the bottom of the screen, and when the user tries to type anything, the keyboard covers the textfield, so the user is unable to see what he/she is typing. To solve this, I would like to get the keyboard input from the user, with every stroke/"type" (everytime the user presses a key), and then display it onto a view, which is located just above the keyboard. If this is not possible, or very complex, you are more than welcome to suggest alternative solutions.
Upvotes: 1
Views: 4208
Reputation: 4513
You need to register as observer for notifications to see when the keyboard appears and disappears. Then you would move your view up on show, or restore it to original on hide.
My implementation moves the whole view up by keyboard height, but if you want you can just move UITextField up.
You can take a look at this tutorial, or see my implementation in the answer:
http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift
Add observers in viewDidAppear
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
You then need the methods to move the view in keyboardWillShow
and keyboardWillHide
new methods.
@objc func keyboardWillShow(_ notification: NSNotification) {
let info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let keyboardHeight:CGFloat = keyboardSize.height
//let animationDuration:CGFloat = info[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
}, completion: nil)
}
And to move the view to original state:
@objc func keyboardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform.identity
}, completion: nil)
}
And at the end remove the observer in viewDidDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Upvotes: 2
Reputation: 1369
The following is how I did for that problem. I hope this method solve your problem.
This is the textfield you want to use.
@IBOutlet var userID : UITextField!
You need to write this function.
func textFieldShouldReturn(textField: UITextField!)-> Bool
{ userID.resignFirstResponder( )
return true
}
In your desired function, you need to write this syntax.
@IBAction func login(sender: AnyObject)
{
userID.resignFirstResponder( )
}
This is in ViewDidLoad or ViewDidAppear
override func viewDidLoad( )
{
userID.delegate = self;
}
I hope this is useful for you.
Upvotes: 1
Reputation: 5083
Although Stefan’s Answer is better for your current implementation. Your question was how to get every stroke a user types.
For the sake of completion
class viewController: UIViewController, UITextFieldDelegate{
var textField = UITextField()
override func viewDidLoad() {
textField.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//string is used here as the value of the textField.
//This will be called after every user input in the textField
}
}
string
can be used to see what the latest user input is. You could store a local variable with the current value and compare it to the “new” value to see what has been changed.
Upvotes: 0
Reputation: 770
To go along with Stefan Salatics answer I use the following code as the keyboard will often times still cover the textfield.
-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard
self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height));
[UIView commitAnimations];
}
Upvotes: 1