Objective-C: ScrollView and keyboard visibility
I was just trying to create an user-form inside a scrollview. On the bigger screens the form does not scroll if no keyboard is shown, on the smaller screens it does. But as soon as the keyboard is visible it should start scrolling on all screens. Meaning that the ScrollView’s height should be reduced with a certain amount (the height of the keyboard) pixels.
I’ve been looking around on the net for a bit came up with this solution:
1. First of all we need to have an IBOutlet reference to both the UIScrollView and the NSLayoutConstraint that references the bottom constraint of the UIScrollView.
1. First we need to know when the keyboard is showing/hiding, we can do that with the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification observers that we register in the viewDidLoad.
2. When the keyboard shows we need to calculate the height of the keyboard and set that height to the bottom constraint of the UIScrollView.
3. When the keyboard is hiding the bottom constraint of the UIScrollView needs to go back to zero.
And that’s all there is to solve this!
Remark: In my example I’ve set the constraint to a negative value, it depends on how you define your constraint, so in your example it might be that the bottom constraint needs to be positive value.