Archived
Setup screen scrolls up when keyboard would block textfields; fixed bug that would prevent a substitution from being shown if it contained a question mark (e.g.: 'INF7?inf999')
This commit is contained in:
@@ -20,6 +20,8 @@ class FirstTimeViewController: UIViewController, UITextFieldDelegate {
|
||||
@IBOutlet weak var defaultPlanSegmentedControl: UISegmentedControl!
|
||||
@IBOutlet weak var textToolbar: UIToolbar!
|
||||
|
||||
var activeTextField: UITextField?
|
||||
|
||||
@IBAction func gradeInfoButton(_ sender: UIButton) {
|
||||
self.present(self.df.getInfoAlert(for: "grade"), animated: true)
|
||||
}
|
||||
@@ -34,6 +36,54 @@ class FirstTimeViewController: UIViewController, UITextFieldDelegate {
|
||||
|
||||
func textFieldDidBeginEditing(_ textField: UITextField) {
|
||||
textField.inputAccessoryView = textToolbar
|
||||
self.activeTextField = textField
|
||||
}
|
||||
|
||||
func textFieldDidEndEditing(_ textField: UITextField) {
|
||||
self.activeTextField = nil
|
||||
}
|
||||
|
||||
func registerForKeyboardNotifications(){
|
||||
//Adding notifies on keyboard appearing
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||
}
|
||||
|
||||
func deregisterFromKeyboardNotifications(){
|
||||
//Removing notifies on keyboard appearing
|
||||
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||
}
|
||||
|
||||
@objc func keyboardWasShown(notification: NSNotification){
|
||||
//Need to calculate keyboard exact size due to Apple suggestions
|
||||
self.scrollView.isScrollEnabled = true
|
||||
let info = notification.userInfo!
|
||||
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
|
||||
let contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height + self.textToolbar.frame.height, right: 0.0)
|
||||
|
||||
self.scrollView.contentInset = contentInsets
|
||||
self.scrollView.scrollIndicatorInsets = contentInsets
|
||||
|
||||
var aRect: CGRect = self.view.frame
|
||||
aRect.size.height -= keyboardSize!.height
|
||||
if let activeField = self.activeTextField {
|
||||
if (!aRect.contains(activeField.frame.origin)) {
|
||||
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc func keyboardWillBeHidden(notification: NSNotification){
|
||||
//Once keyboard disappears, restore original positions
|
||||
let info = notification.userInfo!
|
||||
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
|
||||
let contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: -(keyboardSize!.height + self.textToolbar.frame.height), right: 0.0)
|
||||
self.scrollView.contentInset = contentInsets
|
||||
self.scrollView.scrollIndicatorInsets = contentInsets
|
||||
self.view.endEditing(true)
|
||||
self.scrollView.isScrollEnabled = false
|
||||
}
|
||||
|
||||
@IBAction func finishSetupButton(_ sender: UIBarButtonItem) {
|
||||
@@ -50,6 +100,11 @@ class FirstTimeViewController: UIViewController, UITextFieldDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
self.registerForKeyboardNotifications()
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
@@ -59,4 +114,8 @@ class FirstTimeViewController: UIViewController, UITextFieldDelegate {
|
||||
self.scrollView.isDirectionalLockEnabled = true
|
||||
}
|
||||
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
self.deregisterFromKeyboardNotifications()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user