DanielRead
DanielRead

Reputation: 2794

ionic 6 - Chat Screen - Keep scroll to bottom - Dynamic height components

I have an Angular Ionic 6 app with a chat screen & when it loads up I want the ion-content to be scrolled to the bottom.

I know how to scroll the content to the bottom using this.ionContent.scrollToBottom(); but the problem is some of the messages have to load in dynamic content and have a variable height.

What would be the best way of making sure after all of those messages and components are loaded in THEN it scrolls the chat to the bottom?

Upvotes: 0

Views: 201

Answers (1)

Miguel
Miguel

Reputation: 328

I had this problem a few months ago. Sometimes a lot of messages were loaded, and the scroll didn't reach all the way down.

I worked it around with this function in my utils.service.ts:

 async goToBottom(content:IonContent, times:number=1){
   for(let i=0; i<times; i++){
     await this.wait(200);
     await content.scrollToBottom(200)    
   }
 }

So I can call it like this:

this.utilsSvc.goToBottom(this.content, 3);

and make sure it always arrives the bottom.

Upvotes: 1

Related Questions