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

Posted

技术标签:

【中文标题】以编程方式滚动 ScrollView,其中包含 StackView【英文标题】:Programmatically scroll ScrollView with StackView inside of it 【发布时间】:2020-04-11 01:15:23 【问题描述】:

我有一个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

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

【问题讨论】:

参考这个***.com/a/13163543/3501225 【参考方案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

    

【讨论】:

以上是关于以编程方式滚动 ScrollView,其中包含 StackView的主要内容,如果未能解决你的问题,请参考以下文章

IOS swift scrollview以编程方式

需要时以编程方式添加滚动

如何以编程方式将 ScrollView 滚动到底部

我可以在 Android 中以编程方式滚动 ScrollView 吗?

以编程方式滚动到顶部不起作用?

Nativescript 以编程方式滚动到 ScrollView 的底部