I have a ScrollView
with a StackView
inside of it. When the keyboard appears I am changing the bottomConstraint
.
1. view withouth keyboard
2. how it looks if keyboard shows
3. How it should look like
The problem is that I would like to scroll the ScrollView
a bit up but I can not make it work.
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
is NOT working. 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!