Программная прокрутка ScrollView с StackView внутри него - PullRequest
0 голосов
/ 11 апреля 2020

У меня есть ScrollView с StackView внутри. Когда появляется клавиатура, я меняю bottomConstraint.

1. просмотр без клавиатуры
2. как это выглядит, если клавиатура показывает
3. Как это должно выглядеть

enter image description here enter image description here enter image description here

Проблема заключается в что я хотел бы прокрутить ScrollView немного вверх, но я не могу заставить его работать.

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) это НЕ работает. Я пробовал это, как вы можете видеть в коде, но это не имеет никакого эффекта:

Методы Keyboard Observer

var keyboardHeight: CGFloat?
//MARK: keyboardObserver
@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        self.keyboardHeight = keyboardRectangle.height

        if self.passwordWiederholenTextField.isEditing {
            scrollBottomViewConstraint.constant = -(self.keyboardHeight!)
            self.theScrollView.setContentOffset(CGPoint(x: 0, y: 20), animated: true)
            self.view.layoutIfNeeded()
        }

    }
}

@objc func keyboardWillHide(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        self.keyboardHeight = keyboardRectangle.height

        if self.passwordWiederholenTextField.isEditing {
            scrollBottomViewConstraint.constant = 0
            self.view.layoutIfNeeded()
        }

    }
}

Ограничения:

theScrollView.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
theScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
theScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
scrollBottomViewConstraint = theScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
scrollBottomViewConstraint.isActive = true

theStackView.topAnchor.constraint(equalTo: theScrollView.topAnchor).isActive = true
theStackView.leadingAnchor.constraint(equalTo: theScrollView.leadingAnchor).isActive = true
theStackView.trailingAnchor.constraint(equalTo: theScrollView.trailingAnchor).isActive = true
theStackView.widthAnchor.constraint(equalTo: theScrollView.widthAnchor).isActive = true
stackViewBottomConstraint = theStackView.bottomAnchor.constraint(equalTo: theScrollView.bottomAnchor)
stackViewBottomConstraint.isActive = true

Я не смог ничего найти по этому поводу, поэтому, если у кого-то есть идеи, почему он не работает, я очень благодарен!

1 Ответ

0 голосов
/ 11 апреля 2020

С подсказкой @ Аруна мне удалось это сделать. Это мой окончательный код, и он отлично работает:

var keyboardHeight: CGFloat?
//MARK: keyboardObserver
@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        self.keyboardHeight = keyboardRectangle.height

        let activeField: UITextField? = [passwordTextField, passwordWiederholenTextField].first { $0.isFirstResponder }

        switch activeField {
        case passwordTextField:
            let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight! + 130, right: 0)
            theScrollView.contentInset = insets
            theScrollView.scrollIndicatorInsets = insets
            self.view.layoutIfNeeded()

        case passwordWiederholenTextField:
            let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight! + 30, right: 0)
            theScrollView.contentInset = insets
            theScrollView.scrollIndicatorInsets = insets
            self.view.layoutIfNeeded()

        default:
            break
        }

    }
}

@objc func keyboardWillHide(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        self.keyboardHeight = keyboardRectangle.height

        theScrollView.contentInset = UIEdgeInsets.zero
        theScrollView.scrollIndicatorInsets = UIEdgeInsets.zero

    }
}
...