Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserving contentOffset bottom when keyboard show/hides using UICollectionViewController #19

Closed
brunomunizaf opened this issue Sep 8, 2021 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@brunomunizaf
Copy link

Hello there,

Could you help me figuring out how to preserve the contentOffset from the bottom when the keyboard shows up or hides inside a UICollectionViewController? The example provided on the repo doesn't use a UICollectionVC so I figured you had to do all that keyboard listening etc. Without this I'm not being able to push the cells up along w/ the keyboard appearing. The content inset does change so on keyboard appearing I can scroll up and see the last cell but again it doesn't follow the keyboard. When the keyboard gets hidden it does though.

Thanks

@ekazaev ekazaev self-assigned this Sep 9, 2021
@ekazaev ekazaev added the question Further information is requested label Sep 9, 2021
@ekazaev
Copy link
Owner

ekazaev commented Sep 9, 2021

Hi @brunomunizaf

AFAIK UICollectionViewController will do some keyboard adjustments for you, so you don't have to change contentInset manually. But in your case you also want to change a contentOffset.y of your UICollectionView to push the content up when the keyboard appears. Thats what you need to do. So youll have to listen to keyboard notifications anyway. Keep in mind there will be some edge cases when keyboard start to disappear, like if UICollectionView is already at the maximum contentOffset.y - you don't need to do anything, as content will go folow the keyboard automatically. But if the contentOffset.y is somewhere in the middle of the content, you'll have to update contentOffset.y manually again so that content would follow the keyboard. Keep in mind that user may change keyboards and they may have different height even if it is not disappearing. You have to handle that behaviour yourself. Keep in mind that you cant rely on the .adjustedContentInset value at the moment you receive the keyboardWillAppear notification as at this moment UIScrollView hasn't updated it yet.

Best of luck.

PS: I dont see much value to use UICollectionViewController in general as it neither does what you expect it to do, nor allows you to fine tune it's behaviour.

@ekazaev
Copy link
Owner

ekazaev commented Sep 10, 2021

@brunomunizaf I actually played around a bit and found a better way to deal with it to avoid calculations.
Please check it here - jsut replace the ChatViewController in the exampe with this one:
https://gist.github.com/ekazaev/563cdff9984af927784d74c5b5a22101

@brunomunizaf
Copy link
Author

Awesome, really appreciate that you took the time to help me out! I'll take a look at it right now, I believe it does the trick for me. Thanks again!!

@ekazaev
Copy link
Owner

ekazaev commented Sep 13, 2021

NP, @brunomunizaf. Just this approach gives you less control about what is happening. Let me know if it works for you.

@ekazaev
Copy link
Owner

ekazaev commented Sep 15, 2021

Closing due to inactivity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants