I have 3 textfields, but the keyboard covers the textfield when it pops up on each of them. How can I move the textfield up each time the keyboard pops up?
Current Attempt:
The current attempt with the code below gets called, but does not change the layout at all (followed a different StackOverflow answer).
Code Excerpt:
class UploadPhotoViewController: UIViewController, UIImagePickerControllerDelegate, UITextFieldDelegate, UIActionSheetDelegate {
@IBOutlet weak var photoTitleTextField: UITextField! // title textfield
@IBOutlet var photoCommentTextField: UITextField!
@IBOutlet var photoPriceTextField: UITextField!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillShow:"),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillHide:"),
name: UIKeyboardWillHideNotification,
object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
super.viewWillDisappear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
self.photoTitleTextField.delegate = self;
self.photoCommentTextField.delegate = self;
self.photoPriceTextField.delegate = self;
}
}
func goBack()
{
dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true);
return false;
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true);
}
func keyboardWillShow(notification: NSNotification) {
println("will show")
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = keyboardFrame.size.height + 20
})
}
func keyboardWillHide(notification: NSNotification) {
println("will hide")
}
}