Created
June 8, 2015 13:06
-
-
Save loiclefloch/57955da54983c640bf94 to your computer and use it in GitHub Desktop.
Swift Keyboard handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func viewDidLoad() { | |
super.viewDidLoad() | |
// Register to this notifications for handle reduction of the app | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appWillResignActive"), name: UIApplicationWillResignActiveNotification, object: nil) | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appWillTerminate"), name: UIApplicationWillTerminateNotification, object: nil) | |
// Call end editing when the user tap on the screen but not in the keyboard or the text edit | |
var tapGesture = UITapGestureRecognizer(target: self, action: Selector("endEditing:")) | |
// prevents the scroll view from swallowing up the touch event of child buttons | |
tapGesture.cancelsTouchesInView = false | |
self.view.addGestureRecognizer(tapGesture) | |
self.addDoneButtonOnKeyboard() | |
} | |
/* | |
** Add a done button on the top of the keyboard. | |
*/ | |
func addDoneButtonOnKeyboard() { | |
var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50)) | |
doneToolbar.barStyle = UIBarStyle.Default | |
var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) | |
var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("appWillResignActive")) | |
var items = NSMutableArray() | |
items.addObject(flexSpace) | |
items.addObject(done) | |
doneToolbar.items = items as [AnyObject] | |
doneToolbar.sizeToFit() | |
self.commentTextView.inputAccessoryView = doneToolbar | |
} | |
// MARK: text field delegate | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
self.view.endEditing(true) | |
} | |
func textFieldShouldReturn(textField: UITextField) -> Bool { | |
textField.resignFirstResponder() | |
return true | |
} | |
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { | |
if textField == self.dateTextField { | |
return false | |
} | |
return true | |
} | |
// MARK: text view delegate | |
/* | |
** Animate the view to scroll correctly to the text view | |
*/ | |
func textViewDidBeginEditing(textView: UITextView) { | |
UIView.beginAnimations(nil, context: nil) | |
UIView.setAnimationDelegate(self) | |
UIView.setAnimationDuration(0.5) | |
UIView.setAnimationBeginsFromCurrentState(true) | |
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 200.0, self.view.frame.size.width, self.view.frame.size.height) | |
UIView.commitAnimations() | |
} | |
func textViewDidEndEditing(textView: UITextView) { | |
UIView.beginAnimations(nil, context: nil) | |
UIView.setAnimationDelegate(self) | |
UIView.setAnimationDuration(0.5) | |
UIView.setAnimationBeginsFromCurrentState(true) | |
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 200.0, self.view.frame.size.width, self.view.frame.size.height) | |
UIView.commitAnimations() | |
} | |
// MARK: Notifications selectors | |
func appWillResignActive() { | |
self.commentTextView.resignFirstResponder() | |
self.view.endEditing(true) | |
} | |
func appWillTerminate() { | |
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil) | |
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillTerminateNotification, object: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment