Guest User

Untitled

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import UIKit
  2.  
  3. @UIApplicationMain
  4. class AppDelegate: UIResponder, UIApplicationDelegate {
  5.  
  6. var window: UIWindow?
  7.  
  8. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  9.  
  10. window = UIWindow(frame: UIScreen.main.bounds)
  11. window?.rootViewController = ErrorTestsController(nibName: nil, bundle: nil)
  12. window?.makeKeyAndVisible()
  13.  
  14. // Override point for customization after application launch.
  15. return true
  16. }
  17. }
  18.  
  19. class ErrorTestsController: UIViewController {
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22.  
  23. setupUIElements()
  24. }
  25.  
  26. private func setupUIElements() {
  27. view.backgroundColor = UIColor.red
  28. view.addSubview(blankTextView)
  29.  
  30. blankTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  31. blankTextView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
  32. blankTextView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
  33.  
  34. blankTextViewBottomAnchor = blankTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0)
  35. blankTextViewBottomAnchor?.isActive = true
  36. // Whether I use safeAreaLayoutGuide or the regular view's bottomAnchor changes nothing
  37.  
  38. // Add observers to move the blankTextView up when the keyboard appears
  39. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  40. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
  41.  
  42. // add a UITapGestureRecognizer to make the blankTextView becomeFirstResponder and show the keyboard (will also trigger the above NotificationObserver)
  43. view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showAddTextView)))
  44. }
  45.  
  46. // Define reference to bottomAnchor of the UITextView called "blankTextView" (using iOS 9.0 + NSLayoutConstraints)
  47. public var blankTextViewBottomAnchor: NSLayoutConstraint?
  48.  
  49. public var blankTextView: UITextView = {
  50. let textview = UITextView(frame: .zero, textContainer: nil)
  51. textview.translatesAutoresizingMaskIntoConstraints = false
  52. textview.backgroundColor = UIColor.black
  53. return textview
  54. }()
  55.  
  56. @objc private func showAddTextView() {
  57. blankTextView.becomeFirstResponder()
  58. }
  59.  
  60. @objc private func keyboardWillShow(notification: NSNotification) {
  61. guard
  62. let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue,
  63. let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
  64. else { return }
  65.  
  66. blankTextViewBottomAnchor?.constant = -keyboardFrame.height
  67.  
  68. UIView.animate(withDuration: keyboardDuration) {
  69. self.view.layoutIfNeeded()
  70. }
  71. }
  72.  
  73. @objc private func keyboardWillHide(notification: NSNotification) {
  74. guard
  75. let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
  76. else { return }
  77.  
  78. blankTextViewBottomAnchor?.constant = 0.0
  79.  
  80. UIView.animate(withDuration: keyboardDuration) {
  81. self.view.layoutIfNeeded()
  82. }
  83. }
  84. }
Add Comment
Please, Sign In to add comment