Auto Size Comment Cells
Instagram Firebase
Automatically calculating cell sizes for each comment can be somewhat tricky. You'll find many different approaches to solve this common issue in iOS and today I'll present one of the easier methods. We'll first layout the necessary profile image view inside our cell and fetch the user information for our Comment. Finally we'll look at how to use a dummy cell to estimate the necessary height for our cells.

Comments (34)
juliolocoh
6 years ago
Brian can you show us to create those suggested followers
Brian Voong
6 years ago
juliolocoh
6 years ago
ok thks
faraz
6 years ago
Hi Brian, there seems to be a bug in my code that every time I share a photo it appears twice on the home feed. Any idea why?
faraz
6 years ago
EDIT: I think I figured it out it has something to do with following yourself, it fetches the post twice.
Hen Shabat
6 years ago
Hi Brian, How you recommend to deal with delete cell from the collectionView? Thanks!
Hen Shabat
6 years ago
Hi Brian, How you recommend to deal with the last comment - the tf hide it. ( if the screen is full with comments ) Thanks!
omersemizz@gmail.com
6 years ago
Hello Brian, really nice tutorials so far. I've been watching you since couple of months. You're doing real great jobs. Would you be so nice to show us how to show how many posts, followers etc we have in the counter labels and when the following or followers label hit, list all the following and followers. Thank you in advance.
omari
6 years ago
Hey Brian, It would be nice to delete posts, delete comments and edit your profile. This feature is so important.
omari
6 years ago
And also we have the creationDate that we didn't used to sort the comments.
omari
6 years ago
Hello , can someone please tell me why I get the following error message when clicking the submit button to insert a comment : Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
omari
6 years ago
My mistake. My key had the value nil.
omari
6 years ago
Hello Brian, the refreshing of the homefeed doesn't work so well, sometimes when you are refreshing no post is shown. Can you please give us a fix .
Brian Voong
6 years ago
omari
6 years ago
Hey Brian, Firebase works asynchronous is this the difference to traditional servers that you are talking about or what is the difference ? The correct refreshing of the controllers is really important I hope you will show us a solution and if you don't fine an easy one we have to go through the recursion. What is the idea behind the approach with the recursion :S ?
Sanzio Angeli
6 years ago
Howdie Brian! I purchased the course based on your recommendation from YouTube (estimating frame size for the chat bubbles. I was logged in as Chain Networking). I watched this video and am having some trouble implementing this method because the width is not constant for a chat bubble... I want to have the same features as the firebase chat app but be flawless as in the instagram implementation. Can you make some suggestions?
Brian Voong
6 years ago
Sanzio Angeli
6 years ago
Okay... finally got it semi-working. Issue now is the impact on the phone is massive. It takes 10-15 seconds to load 20 or so text messages. Furthermore, when I scroll, it is quite laggy... Know what could be causing the issue? Also welcome back!
Sanzio Angeli
6 years ago
Fixed it with a hack for now... still not perfect but it's good enough :)
luk
6 years ago
Hey Brian, thanks for all the good stuff mate, just a quick one if you have time... I'm trying to apply this same logic to the home feed cells, it works most of the time but sometimes the cells are massive and other times small. It also seems to happen if I navigate away and come back. I've done a lot of research on it, even saw you getting in on it on stackoverflow. I've always had trouble with these autosizing cells. Have you tried it on the home feed cells? Would you do anything differently? Thanks mate, take it easy...
luk
6 years ago
Got it working smoother mate but now it just messes up when the cell is reloaded, let me know what you reckon if you get a chance, thanks mate.
luk
6 years ago
Got this working mate, so no worries. While I'm here though might throw in another question... I'm trying to recreate the effect where images are being loaded and start off as a low res blurry version that gets updated to the higher quality one. I have done this in the past by creating a small version of the image when it's being uploaded but on your video about the Photoselector you achieve this by setting a target size. I've looked around and couldn't find much about it, do you know if this is possible with the Firebase image download url or should I go with the low res version upload? Thanks Mate, appreciate any help you can give.
Tokyojogo
5 years ago
Would like to know this as well Brian. Thanks.
wasim
6 years ago
Hey Brain, How I can hide keyboard after click on submit button ? thanks
Brian Voong
6 years ago
smiller193
6 years ago
Anyone notice that the input accessory view gets stuck at the bottom of the screen if pull up the keyboard and then go back to the previous screen without typing or dismissing the keyboard
smiller193
6 years ago
I don't know if it's my code or if it is just an error
smiller193
6 years ago
im pushing the viewcontroller the same way and popping it with a custom button that i created
dubbasar
5 years ago
Add this line "commentTextField.resignFirstResponder()" in viewWillDisappear method in CommentsController.swift
malrhex
4 years ago
then how to deal with it if he changed the code on lesson #49?
Clive Wong Toh Soon
6 years ago
How do I resize the accessoryview when the text becomes longer than the commentTextField?
stonypig1
6 years ago
hey how are you doing today? i got a question related to this section, i want to show the post comments' count on the post, just like the Like count, but this is even trickier than like count, because when user post a comment is on the different view controller, please give some hint or suggestions how to do it, thanks
kinghenry
5 years ago
so a problem with this after implementing is that the comments are editable after you tap them in the view! im sure its easy enough to set the state of the labels to be uneditable but its a small point to note!
kinghenry
5 years ago
how can we make the comments link to a user's profile?
Brian Voong
5 years ago
kinghenry
5 years ago
Yes, I also notice that when making textView.isEditable = false then I can only tap on the avatar image. I can use self.comments[indexPath.row][“uid”] or something similar. I am using the uid in the constructor of the myPhotos to render either my feed (default) or someone else’s after viewing a comment they made
LinhTa
5 years ago
Hey Brian, I found some small bugs in your app: - In CommentCell, you need to set textField.isEditable = false, otherwise people can just tap on any comment and change its value - Log in and Sign up Controller can't dismiss keyboard. If an user want to sign up instead of log in, he will have to reset the app to dismiss the keyboard
malrhex
4 years ago
how did you solve the log in or sign up dismiss keyboard?
rebeloper
5 years ago
If anyone is using LBTAComponents than this is how you can autosize the comment: override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let frame = CGRect(x: 0, y: 0, width: ScreenSize.width, height: 50) let dummyCell = CommentsDatasourceCell(frame: frame) dummyCell.datasourceItem = commentsDatasource.comments[indexPath.row] dummyCell.setupViews() dummyCell.layoutIfNeeded() let targetSize = CGSize(width: ScreenSize.width, height: 1000) let estimatedSize = dummyCell.systemLayoutSizeFitting(targetSize) return CGSize(width: ScreenSize.width, height: estimatedSize.height) }
caquillo07
5 years ago
Hey! Loved this course, it has helped me a LOT on building my own app. One request, could you make a video on how to add replies to comments? Thank you!!!!!!!
m4hd1
5 years ago
What if we have an image with fix height in top of textview? when i try to do this my cell estimated height sets to 1000 sample -> --------- <-- phone screen(top) [image] <-- cell 1 [text or comment] <-- still cell 1 [image] <-- cell 2 [text or comment] <-- still cell 2 . . -------- <-- phone width screen(bottom)
m4hd1
5 years ago
i mean like home posts but with a autosize cell sometimes my text is short but sometimes is too long
Alex Alx
5 years ago
I don't know how much people will appreciate this video, but I tried doing something like this for at least 3 days and gave up ...Good job Brian !
rehan1531
5 years ago
wow wow wow wow . This is awesome . The best thing in the video is about auto resizing . Hats off sir :-)
ronaldruck@live.com
4 years ago
Amazing Videos Brian!! thanks so much. Can anybody in this channel tell me why whenever y use the Database.fetchUserWithUID(uid: uid, completion: {(user) ........ my collection stops showing all data from Firebase?
ronaldruck@live.com
4 years ago
Silly mistake that took me hours. whenever retreiving posts from a user uid, make sure that user still exists in the "user" child in firebase
malrhex
4 years ago
How to add left and right padding in the containerView that have the photo and the textField to the viewLeft and viewRight anchors? I tried to view.addSubview(containerView) and then set up the Anchors with the padding but it doesn't work
Lucrecio909
4 years ago
If you do not see the comments rendered, try to create a new comment.
malrhex
4 years ago
Hi Brian, do you know can I add this section of code? just to scroll down to the last comment when the textView is show the keyboard. UIView.animate(withDuration: 0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: { self.view.layoutIfNeeded() }, completion: { (completed) in if self.isFirstResponder { let indexPath = NSIndexPath(item: self.comments.count - 1, section: 0) self.collectionView.scrollToItem(at: indexPath as IndexPath, at: .bottom, animated: true) } })
malrhex
4 years ago
where* can I add that piece of code
Brian Voong
4 years ago
malrhex
4 years ago
so, actually you are right, yeah I could learn about notificationCenter, but the difficult part is how am I going to replace that stackView with the cell of the latest comment?
Brian Voong
4 years ago
malrhex
4 years ago
do you have any course with that? I'll buy it. God dam! at this point, I'm not an auto-sufficient developer on swift men.
Brian Voong
4 years ago
malrhex
4 years ago
Thanks, I'll be looking for it. right now it is the 47 top. maybe will be the 48? haha idk! but you make me happier!
Clint Larenz Nurse
4 years ago
Hi Brain, How can I add the time a user uploaded a comment?
RayBee
4 years ago
Has anyone who has taken this course been able to get an app in the App store from the knowledge this course has taught them? Brian, has anyone successfully uploaded an app into the App store from the knowledge gained from taking the time to do all of these lessons? Yes or No!
Brian Voong
4 years ago
Hanisa Mohamed
3 years ago
I have :)
Akinwehinmi Rotimi Joshua
4 years ago
Hi Brian, thank you so much for a wonderful video. You have a mentee in me. Please I need a quick help, please how do I on tap expand and collapse cells in UIcollectionview, on expand it will show more items and on collapse it will show fewer items. Thanks so much for everything
Cinquain
4 years ago
More fire
Clint Larenz Nurse
4 years ago
I’m a little confused on why you set the height to 1000? I tried implementing this for the feed cell so the cell size will change based on the content of the size. But setting the height to 1000 makes it to were I have huge gaps between posts.
Clint Larenz Nurse
3 years ago
Not sure if bug due to IOS13 but dummyCell.layoutIfNeeded() doesn’t seem to work. The text does not drop to the next line it just does ...
Kyle Griffin
3 years ago
Brian, you are a beast!
Kirk Washam
3 years ago
I was able to resolve to auto sizing issue in the sizeForItem function, just set your frame size to your container view frame let frame = containerView.frame let dummyCell = CommentCell(frame: frame) the rest of the function code works as expected. PS, I'm running this on ios 13.2.2 with xcode 11
HELP & SUPPORT