diff --git a/Mastodon/Scene/Onboarding/Login/MastodonLoginView.swift b/Mastodon/Scene/Onboarding/Login/MastodonLoginView.swift index 53ed98997..d59e30dd4 100644 --- a/Mastodon/Scene/Onboarding/Login/MastodonLoginView.swift +++ b/Mastodon/Scene/Onboarding/Login/MastodonLoginView.swift @@ -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) } diff --git a/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift b/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift index fa05a13b5..6ab427e5b 100644 --- a/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift +++ b/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift @@ -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(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