Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- }
- }
- }
Add Comment
Please, Sign In to add comment