Guest User

Untitled

a guest
Nov 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. //: A UIKit based Playground for presenting user interface
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5.  
  6. final class MyViewController: UIViewController {
  7.  
  8. override func loadView() {
  9. let view = UIView()
  10. view.backgroundColor = .white
  11.  
  12. // Add UITextField
  13. let textField = UITextField()
  14. textField.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
  15. textField.placeholder = "Tap to show keyboard"
  16. textField.textColor = .black
  17. view.addSubview(textField)
  18.  
  19. self.view = view
  20. }
  21.  
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24.  
  25. // Add UIButton
  26. let button = UIButton(type: .custom)
  27. button.frame = CGRect(x: 0, y: 0, width: 200, height: 20)
  28. button.setTitle("Tap to hide keyboard", for: UIControl.State())
  29. button.setTitleColor(.red, for: UIControl.State())
  30. button.addTarget(self.view,
  31. action: #selector(UIView.endEditing(_:)),
  32. for: .touchUpInside)
  33. view.addSubview(button)
  34.  
  35. // Setup observing
  36. let notificationCenter = NotificationCenter.default
  37. notificationCenter.addObserver(self,
  38. selector: #selector(keyboardWillShow(_:)),
  39. name: UIApplication.keyboardWillShowNotification, object: nil)
  40. notificationCenter.addObserver(self,
  41. selector: #selector(keyboardWillHide(_:)),
  42. name: UIApplication.keyboardWillHideNotification, object: nil)
  43. }
  44.  
  45. @objc private func keyboardWillShow(_ notification: Notification) {
  46. print("keyboardWillShow: \(String(describing: notification.userInfo))")
  47. }
  48.  
  49. @objc private func keyboardWillHide(_ notification: Notification) {
  50. print("keyboardWillHide: \(String(describing: notification.userInfo))")
  51. }
  52.  
  53. }
  54. // Present the view controller in the Live View window
  55. PlaygroundPage.current.liveView = MyViewController()
Add Comment
Please, Sign In to add comment