Re: Scrolling table view when the keyboard appears
Re: Scrolling table view when the keyboard appears
- Subject: Re: Scrolling table view when the keyboard appears
- From: Doug Hill <email@hidden>
- Date: Tue, 24 May 2016 16:36:32 -0700
> On May 24, 2016, at 1:02 PM, Kyle Sluder <email@hidden> wrote:
>
> On Tue, May 24, 2016, at 12:33 PM, Doug Hill wrote:
>> OK, this might have been more obvious to people, but it finally came to
>> me how to handle the keyboard display.
>>
>> Instead of changing the view size, I move the center of the view up, then
>> apply a content inset to the table to adjust for the part of the table
>> offscreen. Animations seem to work much better now.
>
> Did you really mean “move the center of the view”? Or did you mean
> scrolling the center of the viewport?
>
> Either way, contentInset is definitely the way to handle this.
>
> --Kyle Sluder
When the keyboard comes up, I was trying to adjust the height of the view to be smaller so that it would move out of the way of the keyboard. This had a disadvantage of not keep the scroll offset, e.g. content offset. That is, if the table is scrolled to the bottom, adjusting the height will hide the last rows of the table but not keep the scroll area there.
Instead, I decided to move the view out of the way of the keyboard by adjusting the center. This has the advantage of not adjusting the content offset. That is, the table is scrolled to the same row. However, after the view is moved, part of it will be clipped by the superview since the view was moved above the origin of the superview. Adjusting the content inset of the table view will cause the content to move down so that it won’t be clipped. And scrolling will stop at the first row in the table.
Doug Hill
>>
>> Doug Hill
>>
>>> On May 20, 2016, at 5:21 PM, Doug Hill <email@hidden> wrote:
>>>
>>> I’m implementing a chat message view with a table view and a text field underneath it. I want the most recent messages at the bottom, and the scroll position always to stay at the last row in the table.
>>>
>>> There are some tricks to making sure the table scrolls to the bottom when it’s first drawn and when adding new rows. I seem to be able to make this work but I’m running into a problem keeping the table scrolled to the bottom when the keyboard is shown. I listen for the 'keyboard shown’ notification and adjust the height of my entire view by the height of keyboard. This works but the scroll position ends up wrong, as it doesn’t keep the same scroll position at the last row when the view is moved up. OK, so I set the contentOffset to compensate for this.
>>>
>>> Here is the code I use in the Keyboard Will show notification:
>>>
>>> [UIView animateWithDuration:keyboardAnimDuration
>>> animations:
>>> ^{
>>> self.view.frame = newViewFrame;
>>> }
>>> completion:^(BOOL finished)
>>> {
>>> [self.myTableView setContentOffset:newContentOffset animated:NO];
>>> }];
>>>
>>> So far, things work pretty well and things end up in the right place with. However, the animation is weird.
>>>
>>> • Even though the view frame is changed inside the animation block, it moves into place immediately with no animation. No matter what duration I set it seems to be ignored.
>>> • Then the keyboard scrolls into place. Because my view has already been moved up, you see a blank space where the keyboard moves up and over. This looks pretty bad.
>>> • The table scrolls with an animation even though contentOffset is changed in the completion block and I set the animated param to NO.
>>> • The time for the table scroll to animate is fixed.
>>>
>>> Here is an example of this animation:
>>>
>>> https://youtu.be/SBSts2UOJXw
>>>
>>> (Try on slow speed to see it better)
>>>
>>> I know this can be done correctly because I see other iOS chat apps that make the list view move up with the keyboard in a coordinated, smooth animation. Any ideas?
>>>
>>> Doug
>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> Please do not post admin requests or moderator comments to the list.
>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden <mailto:email@hidden>)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com <http://lists.apple.com/>
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden <mailto:email@hidden>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden <mailto:email@hidden>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com <http://lists.apple.com/>
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden <mailto:email@hidden>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden