Do the keyboard-dance (#540)

This commit is contained in:
Nathan Mattes 2022-11-13 14:30:05 +01:00
parent f293d17884
commit e7b8870329
2 changed files with 42 additions and 1 deletions

View File

@ -19,6 +19,7 @@ class MastodonLoginView: UIView {
let tableView: UITableView
private var tableViewWrapper: UIView
let navigationActionView: NavigationActionView
var bottomConstraint: NSLayoutConstraint?
override init(frame: CGRect) {
@ -50,6 +51,7 @@ class MastodonLoginView: UIView {
tableView = ContentSizedTableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = Asset.Scene.Onboarding.textFieldBackground.color
tableView.keyboardDismissMode = .onDrag
tableViewWrapper = UIView()
tableViewWrapper.translatesAutoresizingMaskIntoConstraints = false
@ -77,6 +79,9 @@ class MastodonLoginView: UIView {
}
private func setupConstraints() {
let bottomConstraint = safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: navigationActionView.bottomAnchor)
let constraints = [
headerStackView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
@ -100,8 +105,10 @@ class MastodonLoginView: UIView {
navigationActionView.leadingAnchor.constraint(equalTo: leadingAnchor),
navigationActionView.trailingAnchor.constraint(equalTo: trailingAnchor),
bottomAnchor.constraint(equalTo: navigationActionView.bottomAnchor),
bottomConstraint,
]
self.bottomConstraint = bottomConstraint
NSLayoutConstraint.activate(constraints)
}

View File

@ -46,6 +46,7 @@ class MastodonLoginViewController: UIViewController {
viewModel.delegate = self
navigationItem.hidesBackButton = true
//TODO: @zeitschlag consider keyboard, do the notification-dance
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
@ -66,6 +67,9 @@ class MastodonLoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
let dataSource = UITableViewDiffableDataSource<MastodonLoginViewSection, Mastodon.Entity.Server>(tableView: contentView.tableView) { [weak self] tableView, indexPath, itemIdentifier in
guard let cell = tableView.dequeueReusableCell(withIdentifier: MastodonLoginServerTableViewCell.reuseIdentifier, for: indexPath) as? MastodonLoginServerTableViewCell,
let self = self else {
@ -191,6 +195,36 @@ class MastodonLoginViewController: UIViewController {
contentView.navigationActionView.nextButton.isEnabled = false
}
}
// MARK: - Notifications
@objc func keyboardWillShowNotification(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let keyboardFrameValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber
else { return }
let adjustmentHeight = keyboardFrameValue.cgRectValue.height - view.safeAreaInsets.bottom
contentView.bottomConstraint?.constant = adjustmentHeight
UIView.animate(withDuration: duration.doubleValue, delay: 0, options: .curveEaseInOut) {
self.view.layoutIfNeeded()
}
}
@objc func keyboardWillHideNotification(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber
else { return }
contentView.bottomConstraint?.constant = 0
UIView.animate(withDuration: duration.doubleValue, delay: 0, options: .curveEaseInOut) {
self.view.layoutIfNeeded()
}
}
}
// MARK: - OnboardingViewControllerAppearance