diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index b96a240..deea6b1 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -11,6 +11,7 @@ class FormatLogViewController: UIViewController { private var textView: UITextView! private var formatButton: UIButton! + private var doneButton: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() @@ -27,8 +28,11 @@ class FormatLogViewController: UIViewController { textView.backgroundColor = .white textView.layer.cornerRadius = 12 textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12) + textView.delegate = self view.addSubview(textView) + doneButton = UIBarButtonItem(title: "完成", style: .prominent, target: self, action: #selector(didTapDoneButton)) + formatButton = UIButton(type: .system) formatButton.translatesAutoresizingMaskIntoConstraints = false formatButton.setTitle("格式化", for: .normal) @@ -50,4 +54,19 @@ class FormatLogViewController: UIViewController { formatButton.heightAnchor.constraint(equalToConstant: 52) ]) } + + @objc private func didTapDoneButton() { + textView.resignFirstResponder() + } +} + +extension FormatLogViewController: UITextViewDelegate { + + func textViewDidBeginEditing(_ textView: UITextView) { + navigationItem.rightBarButtonItem = doneButton + } + + func textViewDidEndEditing(_ textView: UITextView) { + navigationItem.rightBarButtonItem = nil + } }