我有一个ScrollView
,里面有一个StackView
。当键盘出现时,我正在更改bottomConstraint
。
1。查看无键盘2。键盘显示时的外观3。它应该是什么样子
问题是我想稍微向上滚动ScrollView
,但我无法使其工作。
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
正在NOT工作。我已经尝试过了,正如您在代码中看到的那样,但是没有效果:
键盘观察者方法
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 { $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
}
}