title: Ionic Scroll Position in iOS
body: <p>I'm building a simple chat application and I'm facing an issue with setting the scroll position in iOS. It seems to work fine on other platforms, including browser. Ionic v5, using Capacitor, building with Appflow.</p>
  
  <p>The container needs to be scrolled to the bottom by default when the data loads. The page then listens for new messages. If a new message is added then the page should scroll to the bottom if the scroll position is within the bottom 150% of the client height. If not, a FAB appears indicating a new message has appeared and the user can select the button to scroll to the button.</p>
  
  <p>The markup is as so. I have a custom domChange directive which listens to dom changes in the div. I prefer to put the scroll logic here as I was experiencing bugs putting it in the message listener before.</p>
  
  <pre><code>&lt;ion-content&gt;
    &lt;div class="chat-container" (click)="containerClick();" (scroll)="scroll($event)" #chatContainer&gt;
      &lt;div class="wrapper" *ngIf="messages.length &gt; 0" (domChange)="onDomChange($event)"&gt;
        &lt;!-- Excuse my inline styling, this is just for debug --&gt;
        &lt;div style="width:100%;text-align:center;padding:20px;" *ngIf="loadingMore"&gt;&lt;ion-spinner&gt;&lt;/ion-spinner&gt;&lt;/div&gt;
        &lt;ng-container *ngFor="let message of messages"&gt;
          &lt;chat-bubble
          [sent]="message.from === authService.user.id"
          [delivered]="message.delivered"
          [position]="message.position"
          [timestamp]="message.createdAt.getTime()"
          [content]="message.content"
          &gt;
          &lt;/chat-bubble&gt;
        &lt;/ng-container&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;ion-fab vertical="bottom" horizontal="center" slot="fixed" *ngIf="showScrollBtn"&gt;
      &lt;ion-fab-button size="small" (click)="scrollToBottom()"&gt;&lt;ion-icon name="arrow-down-outline"&gt;&lt;/ion-icon&gt;&lt;/ion-fab-button&gt;
    &lt;/ion-fab&gt;
  &lt;/ion-content&gt;
  </code></pre>
  
  <p>The CSS is relatively simple but I am use a column-reverse flexbox and I think this might be one of the causes to the issue. The column-reverse means the webview renders the scroll at the bottom by default.</p>
  
  <pre><code>.chat-container {
    height: 100%;
    flex-grow: 1;
    overflow: auto;
    display: flex;
    flex-direction: column-reverse;
    overflow-x: hidden;
    padding: 0 16px 0 16px;
  }
  </code></pre>
  
  <p>And then the relevant functions are:</p>
  
  <pre><code>  public onDomChange(event: any) {
      if(event.addedNodes.length &gt; 0 &amp;&amp; event.addedNodes[0].localName == 'chat-bubble') {
        const scrollTop = (this.platform.is('ios') &amp;&amp; !this.platform.is('mobileweb')) ? this.chatContainer.nativeElement.scrollTop + this.chatContainer.nativeElement.scrollHeight : this.chatContainer.nativeElement.scrollTop;
        /*
         * Because of the column-reverse iOS sees the bottom of the div as scrollTop = 0 and the top of the div as scrollTop = -scrollHeight
         * Android sees it as scrollTop = scrollHeight at the bottom and scrollTop = 0 at the top
        */
        const scrollHeight = this.chatContainer.nativeElement.scrollHeight;
        const clientHeight = this.chatContainer.nativeElement.clientHeight * 1.5;
  
        const newMessageIndex = this.messages[this.messages.length - 1].index;
  
        if((this.messages.length &lt;= 30 || scrollHeight - scrollTop &lt;= clientHeight) &amp;&amp; this.channel.lastConsumedMessageIndex &lt; newMessageIndex) {
          this.scrollToBottom();
        } else if(this.channel.lastConsumedMessageIndex &lt; newMessageIndex) {
          this.showScrollBtn = true;
        }
      }
    }
  </code></pre>
  
  <pre><code>  public scrollToBottom(): Promise&lt;any&gt; {
      this.showScrollBtn = false;
      this.chatContainer.nativeElement.scrollTop = this.chatContainer.nativeElement.scrollHeight;
      return this.channel.setAllMessagesConsumed();
    }
  </code></pre>
  
  <p>The <code>scrollToBottom()</code> function works absolutely fine when triggered by the button click event but doesn't seem to do anything when triggered in the domChange trigger. I think this is because the trigger is called before the chat bubble element has rendered but I'm not sure how to fix this.</p>
  
  <p>Would appreciate any ideas/thoughts</p>
  
  <p>Thanks in advance</p>