Skip to content

Instantly share code, notes, and snippets.

@bnickel
Created May 17, 2018 17:56
Show Gist options
  • Save bnickel/b26a1707c02af3cf00b39cfeb69afc61 to your computer and use it in GitHub Desktop.
Save bnickel/b26a1707c02af3cf00b39cfeb69afc61 to your computer and use it in GitHub Desktop.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ErrorTestsController(nibName: nil, bundle: nil)
window?.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
}
class ErrorTestsController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupUIElements()
}
private func setupUIElements() {
view.backgroundColor = UIColor.red
view.addSubview(blankTextView)
blankTextView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
blankTextView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
blankTextView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
blankTextViewBottomAnchor = blankTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0)
blankTextViewBottomAnchor?.isActive = true
// Whether I use safeAreaLayoutGuide or the regular view's bottomAnchor changes nothing
// Add observers to move the blankTextView up when the keyboard appears
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// add a UITapGestureRecognizer to make the blankTextView becomeFirstResponder and show the keyboard (will also trigger the above NotificationObserver)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showAddTextView)))
}
// Define reference to bottomAnchor of the UITextView called "blankTextView" (using iOS 9.0 + NSLayoutConstraints)
public var blankTextViewBottomAnchor: NSLayoutConstraint?
public var blankTextView: UITextView = {
let textview = UITextView(frame: .zero, textContainer: nil)
textview.translatesAutoresizingMaskIntoConstraints = false
textview.backgroundColor = UIColor.black
return textview
}()
@objc private func showAddTextView() {
blankTextView.becomeFirstResponder()
}
@objc private func keyboardWillShow(notification: NSNotification) {
guard
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue,
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
else { return }
blankTextViewBottomAnchor?.constant = -keyboardFrame.height
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}
@objc private func keyboardWillHide(notification: NSNotification) {
guard
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
else { return }
blankTextViewBottomAnchor?.constant = 0.0
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment