<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Max Natchanon on Medium]]></title>
        <description><![CDATA[Stories by Max Natchanon on Medium]]></description>
        <link>https://medium.com/@maxnatchanon?source=rss-141b4f9792aa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*e-0xv3XLuQv22BfNy-RADQ.png</url>
            <title>Stories by Max Natchanon on Medium</title>
            <link>https://medium.com/@maxnatchanon?source=rss-141b4f9792aa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:22:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maxnatchanon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SwiftUI: How to get content offset from ScrollView]]></title>
            <link>https://medium.com/@maxnatchanon/swiftui-how-to-get-content-offset-from-scrollview-5ce1f84603ec?source=rss-141b4f9792aa------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ce1f84603ec</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[scrollview]]></category>
            <category><![CDATA[content]]></category>
            <category><![CDATA[offset]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Max Natchanon]]></dc:creator>
            <pubDate>Fri, 27 Dec 2019 08:16:34 GMT</pubDate>
            <atom:updated>2025-01-11T14:39:36.394Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FTnvv19bnCyyMufSYi9vWw.png" /></figure><p>SwiftUI provides an easy and declarative way to compose UI for our applications. But it is still a very new framework. Many features/APIs in the UIKit, that we are familiar, are still missing.</p><p>In the UIKit, we have an access to UIScrollView’s <strong><em>contentOffset</em></strong><em> </em>and can be notified when it changed via delegate method <strong><em>scrollViewDidScroll(_:)</em></strong>. But we don’t have anything like that in the SwiftUI’s ScrollView, at least for now.</p><p>This feature is very important for some interfaces or interactions. And luckily, Apple provides us enough tools in SwiftUI to do that. So, in this article, I will try to modify the ScrollView to get that function. The usage below is our goal for today.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4ea36f1adf0499c9d2d2e32064a8bc0/href">https://medium.com/media/c4ea36f1adf0499c9d2d2e32064a8bc0/href</a></iframe><p>Here’s the plan. We will use two things, <strong><em>GeometryReader</em></strong> and <strong><em>Preference</em></strong>, to get the content offset of the ScrollView and pass that value up to the binding.</p><p>If you don’t know what GeometryReader or Preference is. I recommended reading these great articles first. <a href="https://swiftui-lab.com/geometryreader-to-the-rescue/">GeometryReader to the Rescue</a> and <a href="https://swiftui-lab.com/communicating-with-the-view-tree-part-1/">Inspecting the View Tree — Part 1: PreferenceKey</a>.</p><p>In short, <em>GeometryReader</em> provides us an access to the container view’s size and coordinate space, which give us more control over the layout of the child views. And <em>Preference</em> is a way to pass the data up the view hierarchy. Just like a publisher-subscriber relationship where the child view sends the data via <em>Preference</em> to the parent who subscribed to that change.</p><h3>Get the content offset</h3><p>Let’s get started. We will try to get the content offset first, just display that inside the scroll view for now. We can do that by using <em>GeometryReader</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3dff2baac5672f5133f486e4c394346/href">https://medium.com/media/d3dff2baac5672f5133f486e4c394346/href</a></iframe><p>We have two <em>GeometryReader</em> here. The inside one is for getting the position of the topmost element in the scroll view, which is the <em>Text</em> in this case. And the outside one is for getting the position of the scroll view.</p><p>Now we can calculate the content offset by subtracting minY of the top-most content from minY of the scroll view. If we don’t have the outside <em>GeometryReader</em> and scroll view’s top safe area inset is not zero, the result will be incorrect since we get the frame using the global coordinate space. You can also try setting custom coordinate space outside and use that to get the frame instead, then remove the outside <em>GeometryReader</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*QZcghx63Mw-_WDYbucolrg.gif" /><figcaption>Displaying the content offset</figcaption></figure><p>The next step is to pass that value up so we can actually use it. But before that, let’s move our code to a new custom view first. I’ll name my custom scroll view <strong><em>TrackableScrollView</em></strong>, since its content offset is …trackable?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20095d99fcd288d9d141ae76caf2c8af/href">https://medium.com/media/20095d99fcd288d9d141ae76caf2c8af/href</a></iframe><p>This custom scroll view is initialized using the same parameters as the default one plus one additional binding parameter — <strong><em>contentOffset</em></strong>.</p><p>It is safer to wrap another view inside <em>GeometryReader</em> instead of our <em>content</em>, because it can cause weird layout issues to the content view, especially when we have another <em>ScrollView</em> in it. We can use ZStack and some non-visible element, like <em>Color.clear</em>, to get the inside geometry.</p><p>Then we can add the support for horizontal scrolling by changing the ZStack’s alignment and the calculation function, as shown above. And finally, wrap our content inside VStack to get the default scroll view behavior.</p><h3>Pass the value up</h3><p>Now we can go to the next step. To pass the value using <em>Preference</em>, we have to create a new custom <em>PreferenceKey</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d864b3638824dd66658aad10958ff6f5/href">https://medium.com/media/d864b3638824dd66658aad10958ff6f5/href</a></iframe><p>Then send the calculated offset to the parent view using <strong><em>preference(key:value:)</em></strong>. On the parent view, get that value using <strong><em>onPreferenceChange(_:perform:)</em></strong> and assign it to the <em>contentOffset</em> binding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0095c2d659e44b4469742eb9c57bd897/href">https://medium.com/media/0095c2d659e44b4469742eb9c57bd897/href</a></iframe><h4>And… our custom scroll view is ready to use!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*2BXPNL_8ORvbEmYMhOF76A.gif" /><figcaption>🎉</figcaption></figure><p>You can download the full source code including the demo view above from here.</p><p><a href="https://github.com/maxnatchanon/trackable-scroll-view">maxnatchanon/trackable-scroll-view</a></p><h3>Note</h3><p>There are some limitations.</p><p>First, <strong>we cannot set its content offset from the parent</strong>.</p><p>And <strong>when</strong> <strong>the content inside the scroll view is changing its size according to content offset in the same direction of the scroll view’s scroll direction, the scroll view will stop working</strong>. For example, when one of the content’s height is a function of scroll view’s content offset and the scroll direction is vertical, the result is shown below. It is a very rare use case for me. But if you face this situation, you might have to find other ways to get the content offset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*QJqNsk09nFeRU7hiN_bvOA.gif" /><figcaption>Trying to expand the height as I scroll down</figcaption></figure><h4>Thanks for reading this article!</h4><p>If you have any suggestions or improvements, please let me know. 👏👏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ce1f84603ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>