-
-
Save iamrare/788dd1ba2ba03b2ddfa2d9448bcf0ade to your computer and use it in GitHub Desktop.
Drop-in UIAlertController replacement with embedded UITextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TextViewAlertController.swift | |
// TextViewAlertController | |
// | |
// Created by Davide Toldo on 01.11.19. | |
// Copyright © 2019 Davide Toldo. All rights reserved. | |
// | |
import UIKit | |
class TextViewAlertController: UIAlertController { | |
let textView: UITextView = { | |
let view = UITextView(frame: CGRect.zero) | |
view.layer.cornerRadius = 5 | |
view.layer.masksToBounds = true | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
var keyboardHeight: CGFloat = 100 { | |
didSet { | |
let height = UIScreen.main.bounds.height | |
let menu = self.view.frame.height | |
let keyb = self.keyboardHeight | |
self.view.frame.origin.y = height-menu-keyb-20 | |
} | |
} | |
convenience init(title: String?) { | |
self.init(title: title, message: "\n\n\n\n\n\n", preferredStyle: .alert) | |
} | |
override var preferredStyle: UIAlertController.Style { | |
return .alert | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(textView) | |
[textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8), | |
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8), | |
textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 64), | |
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64)].forEach({ $0.isActive = true }) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(sender:)), name: UIResponder.keyboardWillShowNotification, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(sender:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(sender:)), name: UIResponder.keyboardWillHideNotification, object: nil) | |
} | |
deinit { | |
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil) | |
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil) | |
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
textView.becomeFirstResponder() | |
UIView.animate(withDuration: 0.3) { | |
let height = UIScreen.main.bounds.height | |
let menu = self.view.frame.height | |
let keyb = self.keyboardHeight | |
self.view.frame.origin.y = height-menu-keyb-20 | |
} | |
} | |
@objc func keyboardChange(sender: Notification) { | |
guard let userInfo = sender.userInfo else { return } | |
let endFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect | |
keyboardHeight = endFrame?.height ?? 100 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment