Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created November 20, 2018 18:45
Show Gist options
  • Save vialyx/4ba60b7396e8d291c6174153d1e4c83e to your computer and use it in GitHub Desktop.
Save vialyx/4ba60b7396e8d291c6174153d1e4c83e to your computer and use it in GitHub Desktop.
//: 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment