Code Monkey home page Code Monkey logo

Comments (2)

tadija avatar tadija commented on August 30, 2024 1

Hi @schmidan, I wrote this small wrapper which works well for me:

import AloeStackView

/// This class adds automatic scroll functionality when keyboard will show or hide.
open class AutoScrollStackViewController: AloeStackViewController {

    // MARK: Properties

    open var defaultContentInset: UIEdgeInsets = .zero {
        didSet {
            updateContentInset(with: defaultContentInset)
        }
    }

    open var keyboardTopMargin: CGFloat = 20

    // MARK: Lifecycle

    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        addObservers()
    }

    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        NotificationCenter.default.removeObserver(self)
    }

    // MARK: Keyboard Observers

    @objc
    open func keyboardWillShow(_ notification: Notification) {
        let keyboardFrameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
        guard var keyboardFrame = keyboardFrameValue?.cgRectValue else { return }
        keyboardFrame = stackView.convert(keyboardFrame, from: nil)
        let keyboardSize = keyboardFrame.size

        let bottomInset = keyboardSize.height + keyboardTopMargin
        let keyboardInset = UIEdgeInsets(
            top: defaultContentInset.top,
            left: defaultContentInset.left,
            bottom: bottomInset,
            right: defaultContentInset.right
        )

        updateContentInset(with: keyboardInset)
    }

    @objc
    open func keyboardWillHide(_ notification: Notification) {
        updateContentInset(with: defaultContentInset)
    }

    // MARK: Helpers

    private func updateContentInset(with inset: UIEdgeInsets) {
        stackView.contentInset = inset
        stackView.scrollIndicatorInsets = inset
    }

    private func addObservers() {
        let nc = NotificationCenter.default
        nc.addObserver(
            self, selector: #selector(keyboardWillShow(_:)),
            name: UIResponder.keyboardWillShowNotification, object: nil
        )
        nc.addObserver(
            self, selector: #selector(keyboardWillHide(_:)),
            name: UIResponder.keyboardWillHideNotification, object: nil
        )
    }

}

Still not sure if something like this should be integrated into AloeStackViewController though (since keyboard is not always needed), but it might be...

from aloestackview.

schmidan avatar schmidan commented on August 30, 2024 1

Thanks a bunch for the wrapper class. I have a similar solution but using composition.

i believe you are right and this should not be the responsibility of the stack view. as such, this issue can be closed

from aloestackview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.