Программно прокрутите ScrollView с помощью StackView внутри него

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

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

введите здесь описание изображения  введите описание изображения здесь  введите описание изображения здесь

Проблема в том, что я хотел бы немного прокрутить 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

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


person Chris    schedule 11.04.2020    source источник
comment
обратитесь к этому stackoverflow.com/a/13163543/3501225   -  person Arun    schedule 11.04.2020


Ответы (1)


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

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

    }
}
person Chris    schedule 11.04.2020