Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: A UIKit based Playground for presenting user interface
- import UIKit
- import PlaygroundSupport
- final class MyViewController: UIViewController {
- override func loadView() {
- let view = UIView()
- view.backgroundColor = .white
- // Add UITextField
- let textField = UITextField()
- textField.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
- textField.placeholder = "Tap to show keyboard"
- textField.textColor = .black
- view.addSubview(textField)
- self.view = view
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Add UIButton
- let button = UIButton(type: .custom)
- button.frame = CGRect(x: 0, y: 0, width: 200, height: 20)
- button.setTitle("Tap to hide keyboard", for: UIControl.State())
- button.setTitleColor(.red, for: UIControl.State())
- button.addTarget(self.view,
- action: #selector(UIView.endEditing(_:)),
- for: .touchUpInside)
- view.addSubview(button)
- // Setup observing
- let notificationCenter = NotificationCenter.default
- notificationCenter.addObserver(self,
- selector: #selector(keyboardWillShow(_:)),
- name: UIApplication.keyboardWillShowNotification, object: nil)
- notificationCenter.addObserver(self,
- selector: #selector(keyboardWillHide(_:)),
- name: UIApplication.keyboardWillHideNotification, object: nil)
- }
- @objc private func keyboardWillShow(_ notification: Notification) {
- print("keyboardWillShow: \(String(describing: notification.userInfo))")
- }
- @objc private func keyboardWillHide(_ notification: Notification) {
- print("keyboardWillHide: \(String(describing: notification.userInfo))")
- }
- }
- // Present the view controller in the Live View window
- PlaygroundPage.current.liveView = MyViewController()
Add Comment
Please, Sign In to add comment