From 61ae50cdfaa909a3597a4ff507230c6745c17684 Mon Sep 17 00:00:00 2001 From: relikd Date: Sat, 4 Apr 2020 00:06:16 +0200 Subject: [PATCH] Enlarge notes above keyboard --- main/Base.lproj/Main.storyboard | 178 +++++++++++++++++--------- main/Recordings/VCEditRecording.swift | 69 +++++++++- 2 files changed, 178 insertions(+), 69 deletions(-) diff --git a/main/Base.lproj/Main.storyboard b/main/Base.lproj/Main.storyboard index 1eb78ce..19b0d9a 100644 --- a/main/Base.lproj/Main.storyboard +++ b/main/Base.lproj/Main.storyboard @@ -376,7 +376,7 @@ - + @@ -408,17 +408,17 @@ - + - + + + + + + + + + + + + + + + + @@ -641,6 +690,11 @@ Duration: 60:00 + + + + + diff --git a/main/Recordings/VCEditRecording.swift b/main/Recordings/VCEditRecording.swift index 660db2d..425423e 100644 --- a/main/Recordings/VCEditRecording.swift +++ b/main/Recordings/VCEditRecording.swift @@ -11,9 +11,6 @@ class VCEditRecording: UIViewController, UITextFieldDelegate, UITextViewDelegate @IBOutlet private var inputDetails: UITextView! override func viewDidLoad() { - if deleteOnCancel { // mark as destructive - buttonCancel.tintColor = .systemRed - } inputTitle.placeholder = record.fallbackTitle inputTitle.text = record.title inputNotes.text = record.notes @@ -22,14 +19,20 @@ class VCEditRecording: UIViewController, UITextFieldDelegate, UITextViewDelegate End:\t\t\(record.stop?.asDateTime() ?? "?") Duration:\t\(record.durationString ?? "?") """ + validateSaveButton() + if deleteOnCancel { // mark as destructive + buttonCancel.tintColor = .systemRed + } + UIResponder.keyboardWillShowNotification.observe(call: #selector(keyboardWillShow), on: self) + UIResponder.keyboardWillHideNotification.observe(call: #selector(keyboardWillHide), on: self) } - func textFieldDidChangeSelection(_ _: UITextField) { validateInput() } - func textViewDidChange(_ _: UITextView) { validateInput() } + func textFieldDidChangeSelection(_ _: UITextField) { validateSaveButton() } + func textViewDidChange(_ _: UITextView) { validateSaveButton() } - private func validateInput() { + private func validateSaveButton() { let changed = (inputTitle.text != record.title ?? "" || inputNotes.text != record.notes ?? "") - buttonSave.isEnabled = changed + buttonSave.isEnabled = changed || deleteOnCancel // always allow save for new recordings } @IBAction func didTapSave(_ sender: UIBarButtonItem) { @@ -65,4 +68,56 @@ class VCEditRecording: UIViewController, UITextFieldDelegate, UITextViewDelegate } return true } + + + // MARK: Handle Keyboard & Notes Frame + + private var isEditingNotes: Bool = false + private var keyboardHeight: CGFloat = 0 + + @IBAction func hideKeyboard() { view.endEditing(false) } + + func textViewDidBeginEditing(_ textView: UITextView) { + if textView == inputNotes { + isEditingNotes = true + updateKeyboard() + } + } + + func textViewDidEndEditing(_ textView: UITextView) { + if textView == inputNotes { + isEditingNotes = false + updateKeyboard() + } + } + + @objc func keyboardWillShow(_ notification: NSNotification) { + keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0 + updateKeyboard() + } + + @objc func keyboardWillHide(_ notification: NSNotification) { + keyboardHeight = 0 + updateKeyboard() + } + + private func updateKeyboard() { + guard let parent = inputNotes.superview, let stack = parent.superview else { + return + } + let shouldAdjust = (isEditingNotes && keyboardHeight > 0) + let noteTitle = parent.subviews.first! + noteTitle.isHidden = shouldAdjust + stack.subviews.forEach{ $0.isHidden = (shouldAdjust && $0 != parent) } + + if shouldAdjust { + inputNotes.frame.origin.y = 0 + inputNotes.frame.size.height = view.frame.height - keyboardHeight - stack.frame.minY - 4 + inputNotes.autoresizingMask = .init(arrayLiteral: .flexibleWidth, .flexibleBottomMargin) + } else { + inputNotes.frame.origin.y = noteTitle.frame.height + inputNotes.frame.size.height = parent.frame.height - noteTitle.frame.height + inputNotes.autoresizingMask = .init(arrayLiteral: .flexibleWidth, .flexibleHeight) + } + } }