以编程方式滚动 ScrollView,其中包含 StackView

Programmatically scroll ScrollView with StackView inside of it

我有一个 ScrollView 里面有一个 StackView。当键盘出现时,我正在更改 bottomConstraint

1.不用键盘查看
2。键盘显示
时的外观 3。它应该是什么样子

问题是我想将 ScrollView 向上滚动一点,但我做不到。

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) 工作。正如您在代码中看到的那样,我试过了,但没有效果:

键盘观察器方法

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

我在这方面找不到任何东西,所以如果有人知道为什么它不起作用,我将非常感激!

在@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 { [=10=].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

    }
}