Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Swift Scroll View School Part 9: Keyboard Insets

In this video tutorial you'll learn how to use content insets to make room for the software keyboard.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3911-scroll-view-school/lessons/10

Hi Greg. Your scrollview tutorials are more effective. Thanks for that. Actually i try to apply this in Objective C but the problem is i am getting Compile time error Expression is not assignable. when i try to assign calculated frame height to Scrollview.contentInset.bottom and scrollview.scollviewIndicatorInsets.bottom. I don’t know why.? Can you explain me? why i could not assign a Value to these Struct Vars. And Even i tried like this
self.scrollViewOutlet.contentInset = UIEdgeInsetsMake(self.scrollViewOutlet.contentInset.top, self.scrollViewOutlet.contentInset.left, adjustmentHeight, self.scrollViewOutlet.contentInset.right);
self.scrollViewOutlet.scrollIndicatorInsets = UIEdgeInsetsMake(self.scrollViewOutlet.scrollIndicatorInsets.top, self.scrollViewOutlet.scrollIndicatorInsets.left, adjustmentHeight, self.scrollViewOutlet.scrollIndicatorInsets.bottom);

Its working nice when you tap on the textField. but after dismissing the keyboard ‘Scrollview Indicators are not showing properly’.

Hi Greg,

Found that “keyboardWillHide” and “keyboardWillShow” get called when keyboard is shown and the device rotates.

I found that function “adjustForKeyBoardShow” does not handle rotation very well. Not sure whats going on exactly, but I added the following code before the inserts are made. This checks to see if the keyboard is being shown and if so sets then to zero before setting them to the required value.

if show {
fgScrollView.contentInset.bottom = 0
fgScrollView.scrollIndicatorInsets.bottom = 0
}

This appears to work okay.
I have tested this out on several actual devices and cannot get it to fail.

Is this there a better way of doing this?

Hello Greg,

Thanks for the tutorial, Could you please explain the part at 10:37. Why content inset is not being added when text field is above the keyboard frame?

Why content inset is not being added when text field is above the keyboard frame?

The content inset is still added. You can verify this with the keyboard still up by manually scrolling down. You can get to the bottom of the view, which would be inaccessible without a bottom inset.

What Greg is pointing out is no autoscrolling is done by the device if the textfield is already/still in frame after the keyboard has been presented. Autoscrolling wants to do to the same thing to show the email field, but it runs out of room at the bottom without the padding that we programmatically add.