添加键盘上方浮动完成按钮
This commit is contained in:
@@ -11,6 +11,8 @@ class FormatLogViewController: UIViewController {
|
|||||||
|
|
||||||
private var textView: UITextView!
|
private var textView: UITextView!
|
||||||
private var formatButton: UIButton!
|
private var formatButton: UIButton!
|
||||||
|
private var floatingDoneButton: UIButton!
|
||||||
|
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
|
||||||
private var doneButton: UIBarButtonItem!
|
private var doneButton: UIBarButtonItem!
|
||||||
private var pasteButton: UIBarButtonItem!
|
private var pasteButton: UIBarButtonItem!
|
||||||
private var resetButton: UIBarButtonItem!
|
private var resetButton: UIBarButtonItem!
|
||||||
@@ -65,7 +67,28 @@ class FormatLogViewController: UIViewController {
|
|||||||
formatButton.addTarget(self, action: #selector(didTapFormatButton), for: .touchUpInside)
|
formatButton.addTarget(self, action: #selector(didTapFormatButton), for: .touchUpInside)
|
||||||
view.addSubview(formatButton)
|
view.addSubview(formatButton)
|
||||||
|
|
||||||
|
var doneConfig = UIButton.Configuration.glass()
|
||||||
|
doneConfig.title = "完成"
|
||||||
|
doneConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
|
||||||
|
var outgoing = incoming
|
||||||
|
outgoing.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
|
||||||
|
return outgoing
|
||||||
|
}
|
||||||
|
doneConfig.baseForegroundColor = .label
|
||||||
|
doneConfig.cornerStyle = .capsule
|
||||||
|
doneConfig.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 22, bottom: 15, trailing: 22)
|
||||||
|
floatingDoneButton = UIButton(configuration: doneConfig)
|
||||||
|
floatingDoneButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
floatingDoneButton.alpha = 0
|
||||||
|
floatingDoneButton.addTarget(self, action: #selector(didTapFloatingDoneButton), for: .touchUpInside)
|
||||||
|
view.addSubview(floatingDoneButton)
|
||||||
|
|
||||||
|
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
|
||||||
|
|
||||||
NSLayoutConstraint.activate([
|
NSLayoutConstraint.activate([
|
||||||
|
floatingDoneButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
|
||||||
|
floatingDoneButtonBottomConstraint,
|
||||||
|
|
||||||
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
|
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
|
||||||
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
||||||
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
|
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
|
||||||
@@ -82,6 +105,10 @@ class FormatLogViewController: UIViewController {
|
|||||||
textView.resignFirstResponder()
|
textView.resignFirstResponder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc private func didTapFloatingDoneButton() {
|
||||||
|
textView.resignFirstResponder()
|
||||||
|
}
|
||||||
|
|
||||||
private func setupKeyboardObservers() {
|
private func setupKeyboardObservers() {
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||||
@@ -98,6 +125,9 @@ class FormatLogViewController: UIViewController {
|
|||||||
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
self.textView.contentInset.bottom = keyboardHeight
|
self.textView.contentInset.bottom = keyboardHeight
|
||||||
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
|
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 8)
|
||||||
|
self.floatingDoneButton.alpha = 1
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,6 +137,9 @@ class FormatLogViewController: UIViewController {
|
|||||||
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
self.textView.contentInset.bottom = 0
|
self.textView.contentInset.bottom = 0
|
||||||
self.textView.verticalScrollIndicatorInsets.bottom = 0
|
self.textView.verticalScrollIndicatorInsets.bottom = 0
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -16
|
||||||
|
self.floatingDoneButton.alpha = 0
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ class FormatNoteViewController: UIViewController {
|
|||||||
private var textView: UITextView!
|
private var textView: UITextView!
|
||||||
private var iCloudButton: UIButton!
|
private var iCloudButton: UIButton!
|
||||||
private var noteButton: UIButton!
|
private var noteButton: UIButton!
|
||||||
|
private var floatingDoneButton: UIButton!
|
||||||
|
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
|
||||||
private var doneButton: UIBarButtonItem!
|
private var doneButton: UIBarButtonItem!
|
||||||
private var pasteButton: UIBarButtonItem!
|
private var pasteButton: UIBarButtonItem!
|
||||||
private var resetButton: UIBarButtonItem!
|
private var resetButton: UIBarButtonItem!
|
||||||
@@ -66,10 +68,28 @@ class FormatNoteViewController: UIViewController {
|
|||||||
noteButton.addTarget(self, action: #selector(didTapNoteButton), for: .touchUpInside)
|
noteButton.addTarget(self, action: #selector(didTapNoteButton), for: .touchUpInside)
|
||||||
view.addSubview(noteButton)
|
view.addSubview(noteButton)
|
||||||
|
|
||||||
|
var doneConfig = UIButton.Configuration.glass()
|
||||||
|
doneConfig.title = "完成"
|
||||||
|
doneConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
|
||||||
|
var outgoing = incoming
|
||||||
|
outgoing.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
|
||||||
|
return outgoing
|
||||||
|
}
|
||||||
|
doneConfig.baseForegroundColor = .label
|
||||||
|
doneConfig.cornerStyle = .capsule
|
||||||
|
doneConfig.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 22, bottom: 15, trailing: 22)
|
||||||
|
floatingDoneButton = UIButton(configuration: doneConfig)
|
||||||
|
floatingDoneButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
floatingDoneButton.alpha = 0
|
||||||
|
floatingDoneButton.addTarget(self, action: #selector(didTapFloatingDoneButton), for: .touchUpInside)
|
||||||
|
view.addSubview(floatingDoneButton)
|
||||||
|
|
||||||
let safeArea = view.safeAreaLayoutGuide
|
let safeArea = view.safeAreaLayoutGuide
|
||||||
let buttonHeight: CGFloat = 52
|
let buttonHeight: CGFloat = 52
|
||||||
let spacing: CGFloat = 16
|
let spacing: CGFloat = 16
|
||||||
|
|
||||||
|
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -spacing)
|
||||||
|
|
||||||
NSLayoutConstraint.activate([
|
NSLayoutConstraint.activate([
|
||||||
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: spacing),
|
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: spacing),
|
||||||
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: spacing),
|
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: spacing),
|
||||||
@@ -85,7 +105,10 @@ class FormatNoteViewController: UIViewController {
|
|||||||
noteButton.leadingAnchor.constraint(equalTo: safeArea.centerXAnchor, constant: spacing / 2),
|
noteButton.leadingAnchor.constraint(equalTo: safeArea.centerXAnchor, constant: spacing / 2),
|
||||||
noteButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -spacing),
|
noteButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -spacing),
|
||||||
noteButton.heightAnchor.constraint(equalToConstant: buttonHeight),
|
noteButton.heightAnchor.constraint(equalToConstant: buttonHeight),
|
||||||
noteButton.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -spacing)
|
noteButton.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -spacing),
|
||||||
|
|
||||||
|
floatingDoneButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -spacing),
|
||||||
|
floatingDoneButtonBottomConstraint
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,6 +129,10 @@ class FormatNoteViewController: UIViewController {
|
|||||||
textView.resignFirstResponder()
|
textView.resignFirstResponder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc private func didTapFloatingDoneButton() {
|
||||||
|
textView.resignFirstResponder()
|
||||||
|
}
|
||||||
|
|
||||||
private func setupKeyboardObservers() {
|
private func setupKeyboardObservers() {
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||||
@@ -122,6 +149,9 @@ class FormatNoteViewController: UIViewController {
|
|||||||
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
self.textView.contentInset.bottom = keyboardHeight
|
self.textView.contentInset.bottom = keyboardHeight
|
||||||
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
|
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 8)
|
||||||
|
self.floatingDoneButton.alpha = 1
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,6 +161,9 @@ class FormatNoteViewController: UIViewController {
|
|||||||
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
self.textView.contentInset.bottom = 0
|
self.textView.contentInset.bottom = 0
|
||||||
self.textView.verticalScrollIndicatorInsets.bottom = 0
|
self.textView.verticalScrollIndicatorInsets.bottom = 0
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -16
|
||||||
|
self.floatingDoneButton.alpha = 0
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user