Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
- }
- deinit {
- NSNotificationCenter.defaultCenter().removeObserver(self);
- }
- func keyboardWillShow(notification: NSNotification) {
- if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
- //let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
- var frame = self.ChatField.frame
- frame.origin.y = frame.origin.y - keyboardSize.height + 167
- self.chatField.frame = frame
- println("asdasd")
- }
- }
- class MyViewController: UIViewController {
- // This constraint ties an element at zero points from the bottom layout guide
- @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
- override func viewDidLoad() {
- super.viewDidLoad()
- // Note that SO highlighting makes the new selector syntax (#selector()) look
- // like a comment but it isn't one
- NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
- }
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
- @objc func keyboardNotification(notification: NSNotification) {
- if let userInfo = notification.userInfo {
- let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
- let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
- let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
- let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
- let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
- if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
- self.keyboardHeightLayoutConstraint?.constant = 0.0
- } else {
- self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
- }
- UIView.animate(withDuration: duration,
- delay: TimeInterval(0),
- options: animationCurve,
- animations: { self.view.layoutIfNeeded() },
- completion: nil)
- }
- }
- class MyViewController: UIViewController {
- // This constraint ties an element at zero points from the bottom layout guide
- @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
- override func viewDidLoad() {
- super.viewDidLoad()
- // Note that SO highlighting makes the new selector syntax (#selector()) look
- // like a comment but it isn't one
- NSNotificationCenter.defaultCenter().addObserver(self,
- selector: #selector(self.keyboardNotification(_:)),
- name: UIKeyboardWillChangeFrameNotification,
- object: nil)
- }
- deinit {
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- func keyboardNotification(notification: NSNotification) {
- if let userInfo = notification.userInfo {
- let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
- let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
- let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
- let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
- let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
- if endFrame?.origin.y >= UIScreen.mainScreen().bounds.size.height {
- self.keyboardHeightLayoutConstraint?.constant = 0.0
- } else {
- self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
- }
- UIView.animateWithDuration(duration,
- delay: NSTimeInterval(0),
- options: animationCurve,
- animations: { self.view.layoutIfNeeded() },
- completion: nil)
- }
- }
- func keyboardWasShown(notification: NSNotification) {
- let info = notification.userInfo!
- let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
- UIView.animateWithDuration(0.1, animations: { () -> Void in
- self.bottomConstraint.constant = keyboardFrame.size.height + 20
- })
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
- }
- func keyboardWillShow(sender: NSNotification) {
- self.view.frame.origin.y = -150 // Move view 150 points upward
- }
- func keyboardWillHide(sender: NSNotification) {
- self.view.frame.origin.y = 0 // Move view to original position
- }
- func textFieldDidBeginEditing(textField: UITextField) {
- animateViewMoving(true, moveValue: 100)
- }
- func textFieldDidEndEditing(textField: UITextField) {
- animateViewMoving(false, moveValue: 100)
- }
- func animateViewMoving (up:Bool, moveValue :CGFloat){
- var movementDuration:NSTimeInterval = 0.3
- var movement:CGFloat = ( up ? -moveValue : moveValue)
- UIView.beginAnimations( "animateView", context: nil)
- UIView.setAnimationBeginsFromCurrentState(true)
- UIView.setAnimationDuration(movementDuration )
- self.view.frame = CGRectOffset(self.view.frame, 0, movement)
- UIView.commitAnimations()
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
- }
- func animateWithKeyboard(notification: NSNotification) {
- // Based on both Apple's docs and personal experience,
- // I assume userInfo and its documented keys are available.
- // If you'd like, you can remove the forced unwrapping and add your own default values.
- let userInfo = notification.userInfo!
- let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
- let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
- let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
- let moveUp = (notification.name == UIKeyboardWillShowNotification)
- // baseContraint is your Auto Layout constraint that pins the
- // text view to the bottom of the superview.
- baseConstraint.constant = moveUp ? -keyboardHeight : 0
- let options = UIViewAnimationOptions(rawValue: curve << 16)
- UIView.animateWithDuration(duration, delay: 0, options: options,
- animations: {
- self.view.layoutIfNeeded()
- },
- completion: nil
- )
- }
- // You have to set this up in storyboard first!.
- // It's a vertical spacing constraint between view and bottom of superview.
- @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil);
- }
- deinit {
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- func keyboardNotification(notification: NSNotification) {
- let isShowing = notification.name == UIKeyboardWillShowNotification
- if let userInfo = notification.userInfo {
- let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
- let endFrameHeight = endFrame?.size.height ?? 0.0
- let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
- let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
- let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
- let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
- self.bottomSpacingConstraint?.constant = isShowing ? endFrameHeight : 0.0
- UIView.animateWithDuration(duration,
- delay: NSTimeInterval(0),
- options: animationCurve,
- animations: { self.view.layoutIfNeeded() },
- completion: nil)
- }
- }
- // You have to set this up in storyboard first!.
- // It's a vertical spacing constraint between view and bottom of superview.
- @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Receive(Get) Notification
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)
- self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
- }
- func keyboardNotification(notification: NSNotification) {
- let isShowing = notification.name == UIKeyboardWillShowNotification
- var tabbarHeight: CGFloat = 0
- if self.tabBarController? != nil {
- tabbarHeight = self.tabBarController!.tabBar.frame.height
- }
- if let userInfo = notification.userInfo {
- let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
- let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
- let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
- let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
- let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
- self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
- UIView.animateWithDuration(duration,
- delay: NSTimeInterval(0),
- options: animationCurve,
- animations: { self.view.layoutIfNeeded() },
- completion: nil)
- }
- }
- struct MoveKeyboard {
- static let KEYBOARD_ANIMATION_DURATION : CGFloat = 0.3
- static let MINIMUM_SCROLL_FRACTION : CGFloat = 0.2;
- static let MAXIMUM_SCROLL_FRACTION : CGFloat = 0.8;
- static let PORTRAIT_KEYBOARD_HEIGHT : CGFloat = 216;
- static let LANDSCAPE_KEYBOARD_HEIGHT : CGFloat = 162;
- }
- func textFieldDidBeginEditing(textField: UITextField) {
- let textFieldRect : CGRect = self.view.window!.convertRect(textField.bounds, fromView: textField)
- let viewRect : CGRect = self.view.window!.convertRect(self.view.bounds, fromView: self.view)
- let midline : CGFloat = textFieldRect.origin.y + 0.5 * textFieldRect.size.height
- let numerator : CGFloat = midline - viewRect.origin.y - MoveKeyboard.MINIMUM_SCROLL_FRACTION * viewRect.size.height
- let denominator : CGFloat = (MoveKeyboard.MAXIMUM_SCROLL_FRACTION - MoveKeyboard.MINIMUM_SCROLL_FRACTION) * viewRect.size.height
- var heightFraction : CGFloat = numerator / denominator
- if heightFraction < 0.0 {
- heightFraction = 0.0
- } else if heightFraction > 1.0 {
- heightFraction = 1.0
- }
- let orientation : UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
- if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown) {
- animateDistance = floor(MoveKeyboard.PORTRAIT_KEYBOARD_HEIGHT * heightFraction)
- } else {
- animateDistance = floor(MoveKeyboard.LANDSCAPE_KEYBOARD_HEIGHT * heightFraction)
- }
- var viewFrame : CGRect = self.view.frame
- viewFrame.origin.y -= animateDistance
- UIView.beginAnimations(nil, context: nil)
- UIView.setAnimationBeginsFromCurrentState(true)
- UIView.setAnimationDuration(NSTimeInterval(MoveKeyboard.KEYBOARD_ANIMATION_DURATION))
- self.view.frame = viewFrame
- UIView.commitAnimations()
- }
- func textFieldDidEndEditing(textField: UITextField) {
- var viewFrame : CGRect = self.view.frame
- viewFrame.origin.y += animateDistance
- UIView.beginAnimations(nil, context: nil)
- UIView.setAnimationBeginsFromCurrentState(true)
- UIView.setAnimationDuration(NSTimeInterval(MoveKeyboard.KEYBOARD_ANIMATION_DURATION))
- self.view.frame = viewFrame
- UIView.commitAnimations()
- }
- func textFieldShouldReturn(textField: UITextField) -> Bool {
- textField.resignFirstResponder()
- return true
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "makeSpaceForKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "makeSpaceForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
- }
- func makeSpaceForKeyboard(notification: NSNotification) {
- let info = notification.userInfo!
- let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
- let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
- if notification.name == UIKeyboardWillShowNotification {
- UIView.animateWithDuration(duration, animations: { () -> Void in
- var frame = self.view.frame
- frame.size.height = frame.size.height - keyboardHeight
- self.view.frame = frame
- })
- } else {
- UIView.animateWithDuration(duration, animations: { () -> Void in
- var frame = self.view.frame
- frame.size.height = frame.size.height + keyboardHeight
- self.view.frame = frame
- })
- }
- }
- NSNotificationCenter.defaultCenter().addObserver(self,
- selector: #selector(MessageThreadVC.keyboardWillShow(_:)),
- name: UIKeyboardWillShowNotification,
- object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self,
- selector: #selector(MessageThreadVC.keyboardWillHide(_:)),
- name: UIKeyboardWillHideNotification,
- object: nil)
- func keyboardWillShow(sender: NSNotification) {
- if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
- self.view.frame.origin.y = -keyboardSize.height
- }
- }
- func keyboardWillHide(sender: NSNotification) {
- self.view.frame.origin.y = 0
- }
- deinit {
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- // This constraint ties the text field to the bottom layout guide
- @IBOutlet var textFieldToBottomLayoutGuideConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name:UIKeyboardWillHideNotification, object: nil);
- }
- func keyboardWillShow(sender: NSNotification) {
- if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
- self.textFieldToBottomLayoutGuideConstraint?.constant += keyboardSize.height
- }
- }
- func keyboardWillHide(sender: NSNotification) {
- if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
- self.textFieldToBottomLayoutGuideConstraint?.constant -= keyboardSize.height
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
- self.originalConstraint = self.centerYConstraint.constant
- }
- func keyboardWillShow(sender: NSNotification) {
- self.centerYConstraint.constant += 30
- }
- func keyboardWillHide(sender: NSNotification) {
- self.centerYConstraint.constant = self.originalConstraint
- }
- class AdminLoginViewController: UIViewController,
- UITextFieldDelegate{
- @IBOutlet weak var txtUserName: UITextField!
- @IBOutlet weak var txtUserPassword: UITextField!
- @IBOutlet weak var btnAdminLogin: UIButton!
- private var activeField : UIView?
- var param:String!
- var adminUser : Admin? = nil
- var kbHeight: CGFloat!
- override func viewDidLoad()
- {
- self.addKeyBoardObserver()
- self.addGestureForHideKeyBoard()
- }
- override func viewWillDisappear(animated: Bool) {
- super.viewWillDisappear(animated)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- func addGestureForHideKeyBoard()
- {
- let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
- tapGesture.cancelsTouchesInView = false
- view.addGestureRecognizer(tapGesture)
- }
- func hideKeyboard() {
- self.view.endEditing(true)
- }
- func addKeyBoardObserver(){
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "willChangeKeyboardFrame:",
- name:UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "willChangeKeyboardFrame:",
- name:UIKeyboardWillHideNotification, object: nil)
- }
- func removeObserver(){
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- //MARK:- textfiled Delegate
- func textFieldShouldBeginEditing(textField: UITextField) -> Bool
- {
- activeField = textField
- return true
- }
- func textFieldShouldEndEditing(textField: UITextField) -> Bool
- {
- if activeField == textField
- {
- activeField = nil
- }
- return true
- }
- func textFieldShouldReturn(textField: UITextField) -> Bool {
- if txtUserName == textField
- {
- txtUserPassword.becomeFirstResponder()
- }
- else if (textField == txtUserPassword)
- {
- self.btnAdminLoginAction(nil)
- }
- return true;
- }
- func willChangeKeyboardFrame(aNotification : NSNotification)
- {
- if self.activeField != nil && self.activeField!.isFirstResponder()
- {
- if let keyboardSize = (aNotification.userInfo![UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
- {
- let dy = (self.activeField?.superview?.convertRect((self.activeField?.frame)!, toView: view).origin.y)!
- let height = (self.view.frame.size.height - keyboardSize.size.height)
- if dy > height
- {
- var frame = self.view.frame
- frame.origin.y = -((dy - height) + (self.activeField?.frame.size.height)! + 20)
- self.view.frame = frame
- }
- }
- }
- else
- {
- var frame = self.view.frame
- frame.origin.y = 0
- self.view.frame = frame
- }
- } }
- func registerForKeyboardNotifications()
- {
- //Keyboard
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWasShown), name: UIKeyboardDidShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillBeHidden), name: UIKeyboardDidHideNotification, object: nil)
- }
- func deregisterFromKeyboardNotifications(){
- NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
- }
- func keyboardWasShown(notification: NSNotification){
- let userInfo: NSDictionary = notification.userInfo!
- let keyboardInfoFrame = userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)?.CGRectValue()
- let windowFrame:CGRect = (UIApplication.sharedApplication().keyWindow!.convertRect(self.view.frame, fromView:self.view))
- let keyboardFrame = CGRectIntersection(windowFrame, keyboardInfoFrame!)
- let coveredFrame = UIApplication.sharedApplication().keyWindow!.convertRect(keyboardFrame, toView:self.view)
- let contentInsets = UIEdgeInsetsMake(0, 0, (coveredFrame.size.height), 0.0)
- self.scrollViewInAddCase .contentInset = contentInsets;
- self.scrollViewInAddCase.scrollIndicatorInsets = contentInsets;
- self.scrollViewInAddCase.contentSize = CGSizeMake((self.scrollViewInAddCase.contentSize.width), (self.scrollViewInAddCase.contentSize.height))
- }
- /**
- this method will fire when keyboard was hidden
- - parameter notification: contains keyboard details
- */
- func keyboardWillBeHidden (notification: NSNotification) {
- self.scrollViewInAddCase.contentInset = UIEdgeInsetsZero
- self.scrollViewInAddCase.scrollIndicatorInsets = UIEdgeInsetsZero
- }
- class SignInController: UIViewController , UITextFieldDelegate {
- @IBOutlet weak var scrollView: UIScrollView!
- // outlet declartion
- @IBOutlet weak var signInTextView: UITextField!
- var kbHeight: CGFloat!
- /**
- *
- * @method viewDidLoad
- *
- */
- override func viewDidLoad() {
- super.viewDidLoad()
- self.signInTextView.delegate = self
- }// end viewDidLoad
- /**
- *
- * @method viewWillAppear
- *
- */
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
- }// end viewWillAppear
- /**
- *
- * @method viewDidAppear
- *
- */
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- }// end viewDidAppear
- /**
- *
- * @method viewWillDisappear
- *
- */
- override func viewWillDisappear(animated: Bool) {
- super.viewWillDisappear(animated)
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- /**
- *
- * @method textFieldShouldReturn
- * retun the keyboard value
- *
- */
- // MARK -
- func textFieldShouldReturn(textField: UITextField) -> Bool {
- signInTextView.resignFirstResponder()
- return true;
- }// end textFieldShouldReturn
- // MARK - keyboardWillShow
- func keyboardWillShow(notification: NSNotification) {
- if let userInfo = notification.userInfo {
- if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
- kbHeight = keyboardSize.height
- self.animateTextField(true)
- }
- }
- }// end keyboardWillShow
- // MARK - keyboardWillHide
- func keyboardWillHide(notification: NSNotification) {
- self.animateTextField(false)
- }// end keyboardWillHide
- // MARK - animateTextField
- func animateTextField(up: Bool) {
- var movement = (up ? -kbHeight : kbHeight)
- UIView.animateWithDuration(0.3, animations: {
- self.view.frame = CGRectOffset(self.view.frame, 0, movement)
- })
- }// end animateTextField
- /**
- *
- * @method didReceiveMemoryWarning
- *
- */
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }// end didReceiveMemoryWarning
- }// end SignInController
- func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
- IQKeyboardManager.sharedManager().enable = true
- return true
- }
- //MARK: - Observers
- extension UIViewController {
- func addObserverForNotification(notificationName: String, actionBlock: (NSNotification) -> Void) {
- NSNotificationCenter.defaultCenter().addObserverForName(notificationName, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: actionBlock)
- }
- func removeObserver(observer: AnyObject, notificationName: String) {
- NSNotificationCenter.defaultCenter().removeObserver(observer, name: notificationName, object: nil)
- }
- }
- //MARK: - Keyboard observers
- extension UIViewController {
- typealias KeyboardHeightClosure = (CGFloat) -> ()
- func addKeyboardChangeFrameObserver(willShow willShowClosure: KeyboardHeightClosure?,
- willHide willHideClosure: KeyboardHeightClosure?) {
- NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillChangeFrameNotification,
- object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { [weak self](notification) in
- if let userInfo = notification.userInfo,
- let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue(),
- let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
- let c = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt,
- let kFrame = self?.view.convertRect(frame, fromView: nil),
- let kBounds = self?.view.bounds {
- let animationType = UIViewAnimationOptions(rawValue: c)
- let kHeight = kFrame.size.height
- UIView.animateWithDuration(duration, delay: 0, options: animationType, animations: {
- if CGRectIntersectsRect(kBounds, kFrame) { // keyboard will be shown
- willShowClosure?(kHeight)
- } else { // keyboard will be hidden
- willHideClosure?(kHeight)
- }
- }, completion: nil)
- } else {
- print("Invalid conditions for UIKeyboardWillChangeFrameNotification")
- }
- })
- }
- func removeKeyboardObserver() {
- removeObserver(self, notificationName: UIKeyboardWillChangeFrameNotification)
- }
- }
- override func viewWillDisappear(animated: Bool) {
- super.viewDidDisappear(animated)
- removeKeyboardObserver()
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- addKeyboardChangeFrameObserver(willShow: { [weak self](height) in
- //Update constraints here
- self?.view.setNeedsUpdateConstraints()
- }, willHide: { [weak self](height) in
- //Reset constraints here
- self?.view.setNeedsUpdateConstraints()
- })
- }
- override func viewDidLoad()
- {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("makeSpaceForKeyboard:"), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("makeSpaceForKeyboard:"), name:UIKeyboardWillHideNotification, object: nil);
- }
- deinit{
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- var keyboardIsVisible = false
- override func makeSpaceForKeyboard(notification: NSNotification) {
- let info = notification.userInfo!
- let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
- let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
- if notification.name == UIKeyboardWillShowNotification && keyboardIsVisible == false{
- keyboardIsVisible = true
- UIView.animateWithDuration(duration, animations: { () -> Void in
- var frame = self.view.frame
- frame.size.height = frame.size.height - keyboardHeight
- self.view.frame = frame
- })
- } else if keyboardIsVisible == true && notification.name == UIKeyboardWillShowNotification{
- }else {
- keyboardIsVisible = false
- UIView.animateWithDuration(duration, animations: { () -> Void in
- var frame = self.view.frame
- frame.size.height = frame.size.height + keyboardHeight
- self.view.frame = frame
- })
- }
- }
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourClass.keyboardWillBeShown), name:UIKeyboardWillShowNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourClass.keyboardWillBeHidden), name:UIKeyboardWillHideNotification, object: nil);
- func keyboardWillBeShown(sender: NSNotification)
- {
- // Move the table view
- if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
- {
- let contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
- yourTableView.contentInset = contentInsets;
- yourTableView.scrollIndicatorInsets = contentInsets;
- }
- }
- func keyboardWillBeHidden(sender: NSNotification)
- {
- // Moving back the table view back to the default position
- yourTableView.contentInset = UIEdgeInsetsZero;
- yourTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
- }
- // Portrait
- UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
- // Landscape
- UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
- }
- func keyboardWillShow(_ notification:Notification) {
- if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
- tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
- }
- }
- func keyboardWillHide(_ notification:Notification) {
- if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
- tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
- }
- }
- [enter image description here][1]
- import Foundation
- protocol KeyboardHandler: class {
- var bottomConstraint: NSLayoutConstraint! { get set }
- func keyboardWillShow(_ notification: Notification)
- func keyboardWillHide(_ notification: Notification)
- func startObservingKeyboardChanges()
- func stopObservingKeyboardChanges()
- }
- extension KeyboardHandler where Self: UIViewController {
- func startObservingKeyboardChanges() {
- // NotificationCenter observers
- NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillShow, object: nil, queue: nil) { (notification) in
- self.keyboardWillShow(notification)
- }
- // Deal with rotations
- NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { (notification) in
- self.keyboardWillShow(notification)
- }
- // Deal with keyboard change (emoji, numerical, etc.)
- NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextInputCurrentInputModeDidChange, object: nil, queue: nil) { (notification) in
- self.keyboardWillShow(notification)
- }
- NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil) { (notification) in
- self.keyboardWillHide(notification)
- }
- }
- func keyboardWillShow(_ notification: Notification) {
- let verticalPadding: CGFloat = 20 // Padding between the bottom of the view and the top of the keyboard
- guard let value = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
- let keyboardHeight = value.cgRectValue.height
- // Here you could have more complex rules, like checking if the textField currently selected is actually covered by the keyboard, but that's out of this scope.
- self.bottomConstraint.constant = keyboardHeight + verticalPadding
- UIView.animate(withDuration: 0.1, animations: { () -> Void in
- self.view.layoutIfNeeded()
- })
- }
- func keyboardWillHide(_ notification: Notification) {
- self.bottomConstraint.constant = 0
- UIView.animate(withDuration: 0.1, animations: { () -> Void in
- self.view.layoutIfNeeded()
- })
- }
- func stopObservingKeyboardChanges() {
- NotificationCenter.default.removeObserver(self)
- }
- }
- class FormMailVC: UIViewControlle, KeyboardHandler {
- // MARK: - View controller life cycle
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- startObservingKeyboardChanges()
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- stopObservingKeyboardChanges()
- }
- // NSLayoutConstraints
- @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement