简体   繁体   English

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

[英]Programmatically scroll ScrollView with StackView inside of it

I have a ScrollView with a StackView inside of it.我有一个ScrollView ,里面有一个StackView When the keyboard appears I am changing the bottomConstraint .当键盘出现时,我正在更改bottomConstraint

1. view withouth keyboard 1.无键盘查看
2. how it looks if keyboard shows 2. 键盘显示时的样子
3. How it should look like 3. 它应该是什么样子

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

The problem is that I would like to scroll the ScrollView a bit up but I can not make it work.问题是我想将ScrollView向上滚动一点,但我无法让它工作。

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) is NOT working. scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)工作。 Ive tried it as you can see in the code but it has no effect:正如您在代码中看到的那样,我已经尝试过了,但它没有效果:

Keyboard Observer methods键盘观察者方法

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()
        }

    }
}

Constraints:约束:

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

I couldn't find anything on this so if anyone has any idea why it is not working I am very grateful!我在这方面找不到任何东西,所以如果有人知道它为什么不起作用,我非常感激!

With @Arun's tip I managed to get it done.借助@Arun 的提示,我设法完成了它。 This is my final code and it works perfectly:这是我的最终代码,它运行良好:

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM