Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.36 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
  4. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
  5. }
  6.  
  7. deinit {
  8. NSNotificationCenter.defaultCenter().removeObserver(self);
  9. }
  10.  
  11. func keyboardWillShow(notification: NSNotification) {
  12. if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
  13. //let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
  14.  
  15. var frame = self.ChatField.frame
  16. frame.origin.y = frame.origin.y - keyboardSize.height + 167
  17. self.chatField.frame = frame
  18. println("asdasd")
  19. }
  20. }
  21.  
  22. class MyViewController: UIViewController {
  23.  
  24. // This constraint ties an element at zero points from the bottom layout guide
  25. @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
  26.  
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Note that SO highlighting makes the new selector syntax (#selector()) look
  30. // like a comment but it isn't one
  31. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
  32. }
  33.  
  34. deinit {
  35. NotificationCenter.default.removeObserver(self)
  36. }
  37.  
  38. @objc func keyboardNotification(notification: NSNotification) {
  39. if let userInfo = notification.userInfo {
  40. let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
  41. let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
  42. let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
  43. let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
  44. let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
  45. if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
  46. self.keyboardHeightLayoutConstraint?.constant = 0.0
  47. } else {
  48. self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
  49. }
  50. UIView.animate(withDuration: duration,
  51. delay: TimeInterval(0),
  52. options: animationCurve,
  53. animations: { self.view.layoutIfNeeded() },
  54. completion: nil)
  55. }
  56. }
  57.  
  58. class MyViewController: UIViewController {
  59.  
  60. // This constraint ties an element at zero points from the bottom layout guide
  61. @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
  62.  
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. // Note that SO highlighting makes the new selector syntax (#selector()) look
  66. // like a comment but it isn't one
  67. NSNotificationCenter.defaultCenter().addObserver(self,
  68. selector: #selector(self.keyboardNotification(_:)),
  69. name: UIKeyboardWillChangeFrameNotification,
  70. object: nil)
  71. }
  72.  
  73. deinit {
  74. NSNotificationCenter.defaultCenter().removeObserver(self)
  75. }
  76.  
  77. func keyboardNotification(notification: NSNotification) {
  78. if let userInfo = notification.userInfo {
  79. let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
  80. let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
  81. let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
  82. let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
  83. let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
  84. if endFrame?.origin.y >= UIScreen.mainScreen().bounds.size.height {
  85. self.keyboardHeightLayoutConstraint?.constant = 0.0
  86. } else {
  87. self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
  88. }
  89. UIView.animateWithDuration(duration,
  90. delay: NSTimeInterval(0),
  91. options: animationCurve,
  92. animations: { self.view.layoutIfNeeded() },
  93. completion: nil)
  94. }
  95. }
  96.  
  97. func keyboardWasShown(notification: NSNotification) {
  98. let info = notification.userInfo!
  99. let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
  100.  
  101. UIView.animateWithDuration(0.1, animations: { () -> Void in
  102. self.bottomConstraint.constant = keyboardFrame.size.height + 20
  103. })
  104. }
  105.  
  106. override func viewDidLoad() {
  107. super.viewDidLoad()
  108. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
  109. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
  110. }
  111.  
  112. func keyboardWillShow(sender: NSNotification) {
  113. self.view.frame.origin.y = -150 // Move view 150 points upward
  114. }
  115.  
  116. func keyboardWillHide(sender: NSNotification) {
  117. self.view.frame.origin.y = 0 // Move view to original position
  118. }
  119.  
  120. func textFieldDidBeginEditing(textField: UITextField) {
  121. animateViewMoving(true, moveValue: 100)
  122. }
  123. func textFieldDidEndEditing(textField: UITextField) {
  124. animateViewMoving(false, moveValue: 100)
  125. }
  126.  
  127. func animateViewMoving (up:Bool, moveValue :CGFloat){
  128. var movementDuration:NSTimeInterval = 0.3
  129. var movement:CGFloat = ( up ? -moveValue : moveValue)
  130. UIView.beginAnimations( "animateView", context: nil)
  131. UIView.setAnimationBeginsFromCurrentState(true)
  132. UIView.setAnimationDuration(movementDuration )
  133. self.view.frame = CGRectOffset(self.view.frame, 0, movement)
  134. UIView.commitAnimations()
  135. }
  136.  
  137. override func viewDidLoad() {
  138. super.viewDidLoad()
  139.  
  140. NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
  141. NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
  142. }
  143.  
  144. func animateWithKeyboard(notification: NSNotification) {
  145.  
  146. // Based on both Apple's docs and personal experience,
  147. // I assume userInfo and its documented keys are available.
  148. // If you'd like, you can remove the forced unwrapping and add your own default values.
  149.  
  150. let userInfo = notification.userInfo!
  151. let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
  152. let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
  153. let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
  154. let moveUp = (notification.name == UIKeyboardWillShowNotification)
  155.  
  156. // baseContraint is your Auto Layout constraint that pins the
  157. // text view to the bottom of the superview.
  158.  
  159. baseConstraint.constant = moveUp ? -keyboardHeight : 0
  160.  
  161. let options = UIViewAnimationOptions(rawValue: curve << 16)
  162. UIView.animateWithDuration(duration, delay: 0, options: options,
  163. animations: {
  164. self.view.layoutIfNeeded()
  165. },
  166. completion: nil
  167. )
  168.  
  169. }
  170.  
  171. // You have to set this up in storyboard first!.
  172. // It's a vertical spacing constraint between view and bottom of superview.
  173. @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint!
  174.  
  175. override func viewDidLoad() {
  176. super.viewDidLoad()
  177.  
  178. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil);
  179. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil);
  180. }
  181.  
  182. deinit {
  183. NSNotificationCenter.defaultCenter().removeObserver(self)
  184. }
  185.  
  186. func keyboardNotification(notification: NSNotification) {
  187.  
  188. let isShowing = notification.name == UIKeyboardWillShowNotification
  189.  
  190. if let userInfo = notification.userInfo {
  191. let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
  192. let endFrameHeight = endFrame?.size.height ?? 0.0
  193. let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
  194. let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
  195. let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
  196. let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
  197. self.bottomSpacingConstraint?.constant = isShowing ? endFrameHeight : 0.0
  198. UIView.animateWithDuration(duration,
  199. delay: NSTimeInterval(0),
  200. options: animationCurve,
  201. animations: { self.view.layoutIfNeeded() },
  202. completion: nil)
  203. }
  204. }
  205.  
  206. // You have to set this up in storyboard first!.
  207. // It's a vertical spacing constraint between view and bottom of superview.
  208. @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint!
  209.  
  210. override func viewDidLoad() {
  211. super.viewDidLoad()
  212.  
  213. // Receive(Get) Notification
  214. NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
  215. NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)
  216.  
  217.  
  218. self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
  219. }
  220.  
  221. func keyboardNotification(notification: NSNotification) {
  222. let isShowing = notification.name == UIKeyboardWillShowNotification
  223.  
  224. var tabbarHeight: CGFloat = 0
  225. if self.tabBarController? != nil {
  226. tabbarHeight = self.tabBarController!.tabBar.frame.height
  227. }
  228. if let userInfo = notification.userInfo {
  229. let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
  230. let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
  231. let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
  232. let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
  233. let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
  234. self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
  235. UIView.animateWithDuration(duration,
  236. delay: NSTimeInterval(0),
  237. options: animationCurve,
  238. animations: { self.view.layoutIfNeeded() },
  239. completion: nil)
  240. }
  241. }
  242.  
  243. struct MoveKeyboard {
  244. static let KEYBOARD_ANIMATION_DURATION : CGFloat = 0.3
  245. static let MINIMUM_SCROLL_FRACTION : CGFloat = 0.2;
  246. static let MAXIMUM_SCROLL_FRACTION : CGFloat = 0.8;
  247. static let PORTRAIT_KEYBOARD_HEIGHT : CGFloat = 216;
  248. static let LANDSCAPE_KEYBOARD_HEIGHT : CGFloat = 162;
  249. }
  250.  
  251.  
  252. func textFieldDidBeginEditing(textField: UITextField) {
  253. let textFieldRect : CGRect = self.view.window!.convertRect(textField.bounds, fromView: textField)
  254. let viewRect : CGRect = self.view.window!.convertRect(self.view.bounds, fromView: self.view)
  255.  
  256. let midline : CGFloat = textFieldRect.origin.y + 0.5 * textFieldRect.size.height
  257. let numerator : CGFloat = midline - viewRect.origin.y - MoveKeyboard.MINIMUM_SCROLL_FRACTION * viewRect.size.height
  258. let denominator : CGFloat = (MoveKeyboard.MAXIMUM_SCROLL_FRACTION - MoveKeyboard.MINIMUM_SCROLL_FRACTION) * viewRect.size.height
  259. var heightFraction : CGFloat = numerator / denominator
  260.  
  261. if heightFraction < 0.0 {
  262. heightFraction = 0.0
  263. } else if heightFraction > 1.0 {
  264. heightFraction = 1.0
  265. }
  266.  
  267. let orientation : UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
  268. if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown) {
  269. animateDistance = floor(MoveKeyboard.PORTRAIT_KEYBOARD_HEIGHT * heightFraction)
  270. } else {
  271. animateDistance = floor(MoveKeyboard.LANDSCAPE_KEYBOARD_HEIGHT * heightFraction)
  272. }
  273.  
  274. var viewFrame : CGRect = self.view.frame
  275. viewFrame.origin.y -= animateDistance
  276.  
  277. UIView.beginAnimations(nil, context: nil)
  278. UIView.setAnimationBeginsFromCurrentState(true)
  279. UIView.setAnimationDuration(NSTimeInterval(MoveKeyboard.KEYBOARD_ANIMATION_DURATION))
  280.  
  281. self.view.frame = viewFrame
  282.  
  283. UIView.commitAnimations()
  284. }
  285.  
  286.  
  287. func textFieldDidEndEditing(textField: UITextField) {
  288. var viewFrame : CGRect = self.view.frame
  289. viewFrame.origin.y += animateDistance
  290.  
  291. UIView.beginAnimations(nil, context: nil)
  292. UIView.setAnimationBeginsFromCurrentState(true)
  293.  
  294. UIView.setAnimationDuration(NSTimeInterval(MoveKeyboard.KEYBOARD_ANIMATION_DURATION))
  295.  
  296. self.view.frame = viewFrame
  297.  
  298. UIView.commitAnimations()
  299.  
  300. }
  301.  
  302. func textFieldShouldReturn(textField: UITextField) -> Bool {
  303. textField.resignFirstResponder()
  304. return true
  305. }
  306.  
  307. override func viewDidLoad() {
  308. super.viewDidLoad()
  309.  
  310. NSNotificationCenter.defaultCenter().addObserver(self, selector: "makeSpaceForKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
  311. NSNotificationCenter.defaultCenter().addObserver(self, selector: "makeSpaceForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
  312. }
  313.  
  314. func makeSpaceForKeyboard(notification: NSNotification) {
  315. let info = notification.userInfo!
  316. let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
  317. let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
  318.  
  319. if notification.name == UIKeyboardWillShowNotification {
  320. UIView.animateWithDuration(duration, animations: { () -> Void in
  321. var frame = self.view.frame
  322. frame.size.height = frame.size.height - keyboardHeight
  323. self.view.frame = frame
  324. })
  325. } else {
  326. UIView.animateWithDuration(duration, animations: { () -> Void in
  327. var frame = self.view.frame
  328. frame.size.height = frame.size.height + keyboardHeight
  329. self.view.frame = frame
  330. })
  331. }
  332.  
  333. }
  334.  
  335. NSNotificationCenter.defaultCenter().addObserver(self,
  336. selector: #selector(MessageThreadVC.keyboardWillShow(_:)),
  337. name: UIKeyboardWillShowNotification,
  338. object: nil)
  339. NSNotificationCenter.defaultCenter().addObserver(self,
  340. selector: #selector(MessageThreadVC.keyboardWillHide(_:)),
  341. name: UIKeyboardWillHideNotification,
  342. object: nil)
  343.  
  344. func keyboardWillShow(sender: NSNotification) {
  345. if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
  346. self.view.frame.origin.y = -keyboardSize.height
  347. }
  348. }
  349.  
  350. func keyboardWillHide(sender: NSNotification) {
  351. self.view.frame.origin.y = 0
  352. }
  353.  
  354. deinit {
  355. NSNotificationCenter.defaultCenter().removeObserver(self)
  356. }
  357.  
  358. // This constraint ties the text field to the bottom layout guide
  359. @IBOutlet var textFieldToBottomLayoutGuideConstraint: NSLayoutConstraint!
  360.  
  361. override func viewDidLoad() {
  362. super.viewDidLoad()
  363.  
  364. NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name:UIKeyboardWillShowNotification, object: nil);
  365. NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name:UIKeyboardWillHideNotification, object: nil);
  366. }
  367.  
  368. func keyboardWillShow(sender: NSNotification) {
  369. if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
  370. self.textFieldToBottomLayoutGuideConstraint?.constant += keyboardSize.height
  371. }
  372. }
  373.  
  374. func keyboardWillHide(sender: NSNotification) {
  375. if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
  376. self.textFieldToBottomLayoutGuideConstraint?.constant -= keyboardSize.height
  377. }
  378. }
  379.  
  380. override func viewDidLoad() {
  381. super.viewDidLoad()
  382. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
  383. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
  384. self.originalConstraint = self.centerYConstraint.constant
  385. }
  386.  
  387. func keyboardWillShow(sender: NSNotification) {
  388. self.centerYConstraint.constant += 30
  389. }
  390.  
  391. func keyboardWillHide(sender: NSNotification) {
  392. self.centerYConstraint.constant = self.originalConstraint
  393. }
  394.  
  395. class AdminLoginViewController: UIViewController,
  396. UITextFieldDelegate{
  397.  
  398. @IBOutlet weak var txtUserName: UITextField!
  399. @IBOutlet weak var txtUserPassword: UITextField!
  400. @IBOutlet weak var btnAdminLogin: UIButton!
  401.  
  402. private var activeField : UIView?
  403.  
  404. var param:String!
  405. var adminUser : Admin? = nil
  406. var kbHeight: CGFloat!
  407.  
  408. override func viewDidLoad()
  409. {
  410. self.addKeyBoardObserver()
  411. self.addGestureForHideKeyBoard()
  412. }
  413.  
  414. override func viewWillDisappear(animated: Bool) {
  415. super.viewWillDisappear(animated)
  416. }
  417. override func didReceiveMemoryWarning() {
  418. super.didReceiveMemoryWarning()
  419. }
  420.  
  421. func addGestureForHideKeyBoard()
  422. {
  423. let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
  424. tapGesture.cancelsTouchesInView = false
  425. view.addGestureRecognizer(tapGesture)
  426. }
  427.  
  428. func hideKeyboard() {
  429. self.view.endEditing(true)
  430. }
  431.  
  432. func addKeyBoardObserver(){
  433.  
  434. NSNotificationCenter.defaultCenter().addObserver(self, selector: "willChangeKeyboardFrame:",
  435. name:UIKeyboardWillShowNotification, object: nil)
  436. NSNotificationCenter.defaultCenter().addObserver(self, selector: "willChangeKeyboardFrame:",
  437. name:UIKeyboardWillHideNotification, object: nil)
  438. }
  439.  
  440. func removeObserver(){
  441. NSNotificationCenter.defaultCenter().removeObserver(self)
  442. }
  443.  
  444. //MARK:- textfiled Delegate
  445.  
  446. func textFieldShouldBeginEditing(textField: UITextField) -> Bool
  447. {
  448. activeField = textField
  449.  
  450. return true
  451. }
  452. func textFieldShouldEndEditing(textField: UITextField) -> Bool
  453. {
  454. if activeField == textField
  455. {
  456. activeField = nil
  457. }
  458.  
  459. return true
  460. }
  461.  
  462. func textFieldShouldReturn(textField: UITextField) -> Bool {
  463.  
  464. if txtUserName == textField
  465. {
  466. txtUserPassword.becomeFirstResponder()
  467. }
  468. else if (textField == txtUserPassword)
  469. {
  470. self.btnAdminLoginAction(nil)
  471. }
  472. return true;
  473. }
  474.  
  475. func willChangeKeyboardFrame(aNotification : NSNotification)
  476. {
  477. if self.activeField != nil && self.activeField!.isFirstResponder()
  478. {
  479. if let keyboardSize = (aNotification.userInfo![UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
  480. {
  481. let dy = (self.activeField?.superview?.convertRect((self.activeField?.frame)!, toView: view).origin.y)!
  482.  
  483. let height = (self.view.frame.size.height - keyboardSize.size.height)
  484.  
  485. if dy > height
  486. {
  487. var frame = self.view.frame
  488.  
  489. frame.origin.y = -((dy - height) + (self.activeField?.frame.size.height)! + 20)
  490.  
  491. self.view.frame = frame
  492. }
  493. }
  494. }
  495. else
  496. {
  497. var frame = self.view.frame
  498. frame.origin.y = 0
  499. self.view.frame = frame
  500. }
  501. } }
  502.  
  503. func registerForKeyboardNotifications()
  504. {
  505. //Keyboard
  506. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWasShown), name: UIKeyboardDidShowNotification, object: nil)
  507. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillBeHidden), name: UIKeyboardDidHideNotification, object: nil)
  508.  
  509.  
  510. }
  511. func deregisterFromKeyboardNotifications(){
  512.  
  513. NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
  514. NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
  515.  
  516. }
  517. func keyboardWasShown(notification: NSNotification){
  518.  
  519. let userInfo: NSDictionary = notification.userInfo!
  520. let keyboardInfoFrame = userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)?.CGRectValue()
  521.  
  522. let windowFrame:CGRect = (UIApplication.sharedApplication().keyWindow!.convertRect(self.view.frame, fromView:self.view))
  523.  
  524. let keyboardFrame = CGRectIntersection(windowFrame, keyboardInfoFrame!)
  525.  
  526. let coveredFrame = UIApplication.sharedApplication().keyWindow!.convertRect(keyboardFrame, toView:self.view)
  527.  
  528. let contentInsets = UIEdgeInsetsMake(0, 0, (coveredFrame.size.height), 0.0)
  529. self.scrollViewInAddCase .contentInset = contentInsets;
  530. self.scrollViewInAddCase.scrollIndicatorInsets = contentInsets;
  531. self.scrollViewInAddCase.contentSize = CGSizeMake((self.scrollViewInAddCase.contentSize.width), (self.scrollViewInAddCase.contentSize.height))
  532.  
  533. }
  534. /**
  535. this method will fire when keyboard was hidden
  536.  
  537. - parameter notification: contains keyboard details
  538. */
  539. func keyboardWillBeHidden (notification: NSNotification) {
  540.  
  541. self.scrollViewInAddCase.contentInset = UIEdgeInsetsZero
  542. self.scrollViewInAddCase.scrollIndicatorInsets = UIEdgeInsetsZero
  543.  
  544. }
  545.  
  546. class SignInController: UIViewController , UITextFieldDelegate {
  547.  
  548. @IBOutlet weak var scrollView: UIScrollView!
  549.  
  550. // outlet declartion
  551. @IBOutlet weak var signInTextView: UITextField!
  552.  
  553. var kbHeight: CGFloat!
  554.  
  555. /**
  556. *
  557. * @method viewDidLoad
  558. *
  559. */
  560.  
  561. override func viewDidLoad() {
  562. super.viewDidLoad()
  563.  
  564. self.signInTextView.delegate = self
  565.  
  566. }// end viewDidLoad
  567.  
  568. /**
  569. *
  570. * @method viewWillAppear
  571. *
  572. */
  573.  
  574. override func viewWillAppear(animated: Bool) {
  575. super.viewWillAppear(animated)
  576.  
  577. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
  578.  
  579. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
  580.  
  581. }// end viewWillAppear
  582.  
  583. /**
  584. *
  585. * @method viewDidAppear
  586. *
  587. */
  588.  
  589. override func viewDidAppear(animated: Bool) {
  590. super.viewDidAppear(animated)
  591.  
  592.  
  593. }// end viewDidAppear
  594.  
  595. /**
  596. *
  597. * @method viewWillDisappear
  598. *
  599. */
  600. override func viewWillDisappear(animated: Bool) {
  601. super.viewWillDisappear(animated)
  602. NSNotificationCenter.defaultCenter().removeObserver(self)
  603. }
  604.  
  605. /**
  606. *
  607. * @method textFieldShouldReturn
  608. * retun the keyboard value
  609. *
  610. */
  611.  
  612. // MARK -
  613. func textFieldShouldReturn(textField: UITextField) -> Bool {
  614. signInTextView.resignFirstResponder()
  615. return true;
  616.  
  617. }// end textFieldShouldReturn
  618.  
  619. // MARK - keyboardWillShow
  620. func keyboardWillShow(notification: NSNotification) {
  621. if let userInfo = notification.userInfo {
  622. if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
  623. kbHeight = keyboardSize.height
  624. self.animateTextField(true)
  625. }
  626. }
  627. }// end keyboardWillShow
  628.  
  629. // MARK - keyboardWillHide
  630. func keyboardWillHide(notification: NSNotification) {
  631. self.animateTextField(false)
  632. }// end keyboardWillHide
  633.  
  634. // MARK - animateTextField
  635. func animateTextField(up: Bool) {
  636. var movement = (up ? -kbHeight : kbHeight)
  637.  
  638. UIView.animateWithDuration(0.3, animations: {
  639. self.view.frame = CGRectOffset(self.view.frame, 0, movement)
  640. })
  641. }// end animateTextField
  642.  
  643. /**
  644. *
  645. * @method didReceiveMemoryWarning
  646. *
  647. */
  648.  
  649. override func didReceiveMemoryWarning() {
  650. super.didReceiveMemoryWarning()
  651. // Dispose of any resources that can be recreated.
  652.  
  653. }// end didReceiveMemoryWarning
  654.  
  655.  
  656. }// end SignInController
  657.  
  658. func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  659.  
  660. IQKeyboardManager.sharedManager().enable = true
  661. return true
  662. }
  663.  
  664. //MARK: - Observers
  665. extension UIViewController {
  666.  
  667. func addObserverForNotification(notificationName: String, actionBlock: (NSNotification) -> Void) {
  668. NSNotificationCenter.defaultCenter().addObserverForName(notificationName, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: actionBlock)
  669. }
  670.  
  671. func removeObserver(observer: AnyObject, notificationName: String) {
  672. NSNotificationCenter.defaultCenter().removeObserver(observer, name: notificationName, object: nil)
  673. }
  674. }
  675.  
  676. //MARK: - Keyboard observers
  677. extension UIViewController {
  678.  
  679. typealias KeyboardHeightClosure = (CGFloat) -> ()
  680.  
  681. func addKeyboardChangeFrameObserver(willShow willShowClosure: KeyboardHeightClosure?,
  682. willHide willHideClosure: KeyboardHeightClosure?) {
  683. NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillChangeFrameNotification,
  684. object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { [weak self](notification) in
  685. if let userInfo = notification.userInfo,
  686. let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue(),
  687. let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
  688. let c = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt,
  689. let kFrame = self?.view.convertRect(frame, fromView: nil),
  690. let kBounds = self?.view.bounds {
  691.  
  692. let animationType = UIViewAnimationOptions(rawValue: c)
  693. let kHeight = kFrame.size.height
  694. UIView.animateWithDuration(duration, delay: 0, options: animationType, animations: {
  695. if CGRectIntersectsRect(kBounds, kFrame) { // keyboard will be shown
  696. willShowClosure?(kHeight)
  697. } else { // keyboard will be hidden
  698. willHideClosure?(kHeight)
  699. }
  700. }, completion: nil)
  701. } else {
  702. print("Invalid conditions for UIKeyboardWillChangeFrameNotification")
  703. }
  704. })
  705. }
  706.  
  707. func removeKeyboardObserver() {
  708. removeObserver(self, notificationName: UIKeyboardWillChangeFrameNotification)
  709. }
  710. }
  711.  
  712. override func viewWillDisappear(animated: Bool) {
  713. super.viewDidDisappear(animated)
  714.  
  715. removeKeyboardObserver()
  716. }
  717.  
  718. override func viewWillAppear(animated: Bool) {
  719. super.viewWillAppear(animated)
  720.  
  721. addKeyboardChangeFrameObserver(willShow: { [weak self](height) in
  722. //Update constraints here
  723. self?.view.setNeedsUpdateConstraints()
  724. }, willHide: { [weak self](height) in
  725. //Reset constraints here
  726. self?.view.setNeedsUpdateConstraints()
  727. })
  728. }
  729.  
  730. override func viewDidLoad()
  731. {
  732.  
  733. super.viewDidLoad()
  734. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("makeSpaceForKeyboard:"), name:UIKeyboardWillShowNotification, object: nil);
  735. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("makeSpaceForKeyboard:"), name:UIKeyboardWillHideNotification, object: nil);
  736. }
  737.  
  738. deinit{
  739. NSNotificationCenter.defaultCenter().removeObserver(self)
  740. }
  741.  
  742. var keyboardIsVisible = false
  743. override func makeSpaceForKeyboard(notification: NSNotification) {
  744.  
  745. let info = notification.userInfo!
  746. let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
  747. let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
  748.  
  749. if notification.name == UIKeyboardWillShowNotification && keyboardIsVisible == false{
  750.  
  751. keyboardIsVisible = true
  752.  
  753. UIView.animateWithDuration(duration, animations: { () -> Void in
  754. var frame = self.view.frame
  755. frame.size.height = frame.size.height - keyboardHeight
  756. self.view.frame = frame
  757. })
  758.  
  759. } else if keyboardIsVisible == true && notification.name == UIKeyboardWillShowNotification{
  760.  
  761. }else {
  762. keyboardIsVisible = false
  763.  
  764. UIView.animateWithDuration(duration, animations: { () -> Void in
  765. var frame = self.view.frame
  766. frame.size.height = frame.size.height + keyboardHeight
  767. self.view.frame = frame
  768. })
  769. }
  770. }
  771.  
  772. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourClass.keyboardWillBeShown), name:UIKeyboardWillShowNotification, object: nil);
  773. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourClass.keyboardWillBeHidden), name:UIKeyboardWillHideNotification, object: nil);
  774.  
  775. func keyboardWillBeShown(sender: NSNotification)
  776. {
  777. // Move the table view
  778.  
  779. if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
  780. {
  781. let contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
  782.  
  783. yourTableView.contentInset = contentInsets;
  784.  
  785. yourTableView.scrollIndicatorInsets = contentInsets;
  786. }
  787. }
  788.  
  789. func keyboardWillBeHidden(sender: NSNotification)
  790. {
  791. // Moving back the table view back to the default position
  792.  
  793. yourTableView.contentInset = UIEdgeInsetsZero;
  794.  
  795. yourTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
  796. }
  797.  
  798. // Portrait
  799. UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
  800.  
  801. // Landscape
  802. UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
  803.  
  804. override func viewDidLoad() {
  805. super.viewDidLoad()
  806. // Do any additional setup after loading the view, typically from a nib.
  807.  
  808. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  809.  
  810. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
  811.  
  812. }
  813.  
  814. func keyboardWillShow(_ notification:Notification) {
  815.  
  816. if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
  817. tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
  818. }
  819. }
  820. func keyboardWillHide(_ notification:Notification) {
  821.  
  822. if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
  823. tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
  824. }
  825. }
  826. [enter image description here][1]
  827.  
  828. import Foundation
  829.  
  830. protocol KeyboardHandler: class {
  831.  
  832. var bottomConstraint: NSLayoutConstraint! { get set }
  833.  
  834. func keyboardWillShow(_ notification: Notification)
  835. func keyboardWillHide(_ notification: Notification)
  836. func startObservingKeyboardChanges()
  837. func stopObservingKeyboardChanges()
  838. }
  839.  
  840.  
  841. extension KeyboardHandler where Self: UIViewController {
  842.  
  843. func startObservingKeyboardChanges() {
  844.  
  845. // NotificationCenter observers
  846. NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillShow, object: nil, queue: nil) { (notification) in
  847. self.keyboardWillShow(notification)
  848. }
  849.  
  850. // Deal with rotations
  851. NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { (notification) in
  852. self.keyboardWillShow(notification)
  853. }
  854.  
  855. // Deal with keyboard change (emoji, numerical, etc.)
  856. NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextInputCurrentInputModeDidChange, object: nil, queue: nil) { (notification) in
  857. self.keyboardWillShow(notification)
  858. }
  859.  
  860. NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil) { (notification) in
  861. self.keyboardWillHide(notification)
  862. }
  863. }
  864.  
  865.  
  866. func keyboardWillShow(_ notification: Notification) {
  867.  
  868. let verticalPadding: CGFloat = 20 // Padding between the bottom of the view and the top of the keyboard
  869.  
  870. guard let value = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
  871. let keyboardHeight = value.cgRectValue.height
  872.  
  873. // 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.
  874. self.bottomConstraint.constant = keyboardHeight + verticalPadding
  875.  
  876. UIView.animate(withDuration: 0.1, animations: { () -> Void in
  877. self.view.layoutIfNeeded()
  878. })
  879. }
  880.  
  881.  
  882. func keyboardWillHide(_ notification: Notification) {
  883. self.bottomConstraint.constant = 0
  884.  
  885. UIView.animate(withDuration: 0.1, animations: { () -> Void in
  886. self.view.layoutIfNeeded()
  887. })
  888. }
  889.  
  890.  
  891. func stopObservingKeyboardChanges() {
  892. NotificationCenter.default.removeObserver(self)
  893. }
  894.  
  895. }
  896.  
  897. class FormMailVC: UIViewControlle, KeyboardHandler {
  898.  
  899. // MARK: - View controller life cycle
  900. override func viewWillAppear(_ animated: Bool) {
  901. super.viewWillAppear(animated)
  902. startObservingKeyboardChanges()
  903. }
  904.  
  905. override func viewWillDisappear(_ animated: Bool) {
  906. super.viewWillDisappear(animated)
  907. stopObservingKeyboardChanges()
  908. }
  909.  
  910. // NSLayoutConstraints
  911. @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement