top of page

Beyond the Limitations of SwiftUI: List Inside a ScrollView

Writer: Om Chachad
Om Chachad
Oct 5, 2024
3 min read
Sometimes, the best way to achieve the impossible is to not believe it was impossible in the first place.


For the longest time, I have struggled with implementing this very specific implementation of a List View wrapped inside a ScrollView, in SwiftUI. I wanted to be able to recreate the Reminders App interface in my app, Linkeeper. But, as soon as I wrapped the List in a ScrollView, it would instantly disappear. I ended up settling with a compromise that made only half of my view scrollable, but this wasn’t a pleasant experience and would end up limiting the visible content in certain orientations. I still shipped it anyways, but I kept looking for a better solution. Every few months, for over a year, I’ve been going into that code and trying to find a way to make the entire View scrollable, but I haven’t been able to do anything about it- until today.


Just as I was about to look into the world of UIKit for me to be implement this feature, I made a breakthrough within SwiftUI itself. To understand the solution to this problem, it’s important to understand how a List works.


A List in SwiftUI is scrollable by itself, and is designed to be a full fledged view that takes up all available space. The list, by default, does not have a fixed size, which prevents it from being rendered inside the ScrollView.


One solution that people have often suggested is, using a custom implementation of a List using a simple ForEach loop. However, this comes with a huge drawback. You can no longer take advantage of the native UI components that come prebuilt with SwiftUI, and neither can you use SwiftUI modifiers such as .onDrag, .onDelete, or .swipeActions. These are some very real considerations to be made, since rebuilding a List component can be a waste of perhaps the most important resource that we have as developers: time.


Using modifiers like .scaledToFit on the List works, but it leads to there being two scrollable views inside one another, causing there to be two nested scroll targets, which is far from a pleasant experience.


Well then, what is the ideal solution?


The solution turns out to be rather simple, and requires only a few lines of code. I’m guessing your code looks something like this, right now.



Well, let’s start off by wrapping it in a ScrollView.




As expected, the List will disappear. Now, to limit the size while also ensuring the list doesn’t turn into another scrollable view, we need to limit it to exactly the size of the content inside. As it turns out, more often than not, each element inside your list is likely to have a fixed size. So, we can add a frame with a size of the count of elements inside your List, multiplied by the height of each element in your list. In my case, the height of each element was 65px. You can use GeometryReader to find the exact height of each element, or use trial and error to estimate the right value. Keep some extra headroom on top of the actual height for a better experience. Additionally, we need to add an extra buffer of 200px if there are fewer than 4 elements to ensure better results. Implementing this limit, your code should look something like this:


Not only does this method of finding the height make sure that the List is no longer an infinitely expanding view, but it also ensures that the List is always large enough to make sure the List itself isn’t a scrollable view.


Optionally, if you want to use the Inset Grouped style for list, you will have to add this background color to your ScrollView to maintain continuity throughout the interface.





And that’s all you need. Here’s a before and after video, in my app, Linkeeper. (The phone on the left is the improved implementation, and the one on the right is the older one.)



Hopefully, you now have a better implementation of a List embedded inside a ScrollView, and you've built some intuition about how a List works for you to solve future problems. This is one of the few times where I've come up with a solution to a SwiftUI problem entirely on my own, with basically zero online assistance, and I'm kinda proud of myself for doing that. Breaking out of the limitations of SwiftUI can allow developers to create beautiful experiences for their users, and this is a great example of doing so.

 
 
 

44 Comments


blogcommentsieuviet
9 hours ago

web cá độ bóng đá là chủ đề tôi quan sát khi muốn đánh giá sự nhất quán giữa nhiều nhóm nội dung thể thao trong cùng một hệ thống thông tin. Tôi bắt đầu bằng việc chuyển từ chuyên mục lịch thi đấu sang tin tức, sau đó tìm hiểu những bài phân tích trước trận và phần giải thích tỷ lệ. Tôi chú ý đến cách đặt tiêu đề, phân chia đoạn văn và sử dụng liên kết để người đọc nhận biết chủ đề đang theo dõi. Những bài viết có số liệu cần ghi rõ thời điểm cập nhật, trong khi nhận định cá nhân nên được trình bày tách biệt. Tôi cũng quan tâm đến khả…

Like

blogcommentsieuviet
16 hours ago

đánh bài online là lĩnh vực tôi xem xét khi muốn đánh giá cách những bài phân tích trình bày thông tin kỹ thuật liên quan đến kết quả ngẫu nhiên. Tôi bắt đầu bằng việc tìm hiểu Tài Xỉu MD5, Xóc Đĩa và Bầu Cua để quan sát cách giải thích lịch sử phiên và dữ liệu công bố. Tôi chú ý đến việc các thuật ngữ mã hóa, xác thực và kiểm toán có được phân biệt rõ hay không. Một mã băm xuất hiện trên giao diện không tự chứng minh toàn bộ hệ thống vận hành công bằng. Tôi cũng quan tâm đến việc người đọc có thể kiểm tra nguồn dữ liệu và thời điểm cập…

Like

blogcommentsieuviet
2 days ago

uu88 gợi cho mình suy nghĩ về cách những nội dung có tính chất khác nhau được tổ chức trong cùng một hệ thống. Khi tìm hiểu E-sports, mình chú ý đến cách danh mục này được giới thiệu và vị trí của nó trong khu vực trò chơi. Sau đó, mình quan sát phần đăng nhập để xem các trường thông tin và hướng dẫn được bố trí như thế nào. Mình thường nhìn vào sự khác biệt giữa tiêu đề danh mục và tiêu đề chức năng để nhận biết mục đích từng khu vực. Với mình, cách phân chia có trình tự giúp người xem dễ xác định nơi cần tìm, nhất là khi chuyển từ nội dung…


Like

blogcommentsieuviet
2 days ago

789club là nền tảng tôi tìm hiểu khi muốn đánh giá cách tổ chức nội dung trên trang chủ và khả năng nhận diện những chức năng thường dùng. Tôi bắt đầu bằng việc xem phần giới thiệu tổng quan, sau đó đối chiếu cách phân chia Casino, Bắn cá và Xổ số trong hệ thống danh mục. Tôi quan tâm đến vị trí đăng nhập, biểu tượng hỗ trợ và cách sắp xếp các ô nội dung khi nhiều lựa chọn xuất hiện cùng lúc. Một điểm khác tôi muốn kiểm tra là khả năng quay lại trang chính sau khi mở phần hướng dẫn chi tiết. Những hình ảnh được công bố có thể phản ánh giao diện tại…

Like

blogcommentsieuviet
2 days ago

lô đề online khiến mình chú ý đến cách những yếu tố trực quan được sử dụng để hỗ trợ việc nhận biết thông tin. Mình thường quan sát biểu tượng đánh giá trong bảng xếp hạng, đặc biệt là vị trí những ngôi sao so với phần giới thiệu từng mục. Những ký hiệu này cần có cách giải thích rõ ràng để người đọc hiểu ý nghĩa và phạm vi đánh giá. Mình cũng chú ý đến sự liên kết giữa phần nhận xét ngắn và nội dung review chi tiết. Theo mình, việc trình bày các thành phần theo một thứ tự nhất quán sẽ giúp quá trình đối chiếu thuận tiện và hỗ trợ người xem hiểu…

Like
bottom of page