By the way, we will focus only on vertical stacks, but this also will apply to horizontal ones. Let's see what's the difference between regular VStack and List. Scrolling performance of SwiftUI "collections" is poor, making fairly common app use cases for tvOS impossible to achieve with a good user experience.I started this channel talking about stacks in SwiftUI, today we will talk about another stack: LazyVStack. SwiftUI "collections" scroll smoothly, as do usual UIKit collections. I could sadly not perform the same tests on iPad OS and macOS Big Sur, as I have no test devices running these versions, but the sample project should be ready for those devices too. I also ran the example demo on an iPhone 7 test device running iOS 14 and, while performance is not as good as with UICollection view (views "pop in" when navigating horizontally), the performance seems far better than the one on tvOS. Remark: Tests with LazyHGrid / LazyVGrid show that those views suffer from the same issues. Note that the UICollectionView implementation, on the other hand, scales very well with growing number of rows and columns. I specifically introduced row and column item counts for each demo so that you can play a bit with these parameters if you want. The SwiftUI performance issues can easily be noticed with smaller amounts of cells, e.g. Note that the demo contains 20 rows of 50 items each, which is not unusual (think about Netflix for example). You can see that scrolling performance is poor for the first two SwiftUI-based demos, while it is smooth for the UICollectionView implementation. Just run the example project on a real Apple TV device and navigate the various demos with the remote (all cells are focusable buttons so that you can navigate horizontally and vertically). It's still far behind UITableView but better than List. Yes, the CPU usage drops to 25 with LazyVStack. Implementation with UICollectionView and a compositional layout with orthogonally scrollable sections. Replacing the List with a ScrollView and LazyVStack gives you performance similar to the tableview, since that works the same way, rendering only the visible rows. These components are used in a similar way to regular HStack and VStack components but. Same implementation, but with the new LazyVStack and LazyHStack introduced with iOS and tvOS 14.ģ. SwiftUI 2.0 introduced the LazyHStack and LazyVStack components. A VStack / HStack-based implementation.Ģ. This sample project includes 3 implementations for the layout described above:ġ. While the lazy stack should avoid memory growing up unnecessarily as content is browsed, both the normal as well as the lazy variants of stacks suffer from such issues when nested in scroll views.Ī sample project running on iOS, iPadOS and tvOS is attached to this issue. LazyStacks In SwiftUI (SwiftUI LazyHStack, SwiftUI LazyVStack, LazyStacks For Beginners)In this video, we're going to look at lazystacks in SwiftUI. In the following example, a ScrollView contains a LazyVStack that consists of a vertical row of text views. From an old UIKit app developer perspective, it seems as if no reuse mechansim is in place in such cases. items until it needs to render them onscreen. In VStack, all views are rendered and loaded in memory as soon as view is initialized and appears on screen. Unfortunately, the performance obtained with such layouts is very poor.Īs can be seen with Instruments in a very simple case displaying trivial views (colored rectangles), the issue is apparently not related to SwiftUI diffing or view body calculations, but from the rest of the drawing process. ‘Lazy’ keyword distinguishes LazyVStack from VStack. This kind of layout should be easy to reproduce with SwiftUI by nesting several HStacks into a single VStack (or lazy stacks for tvOS 14), themselves nested in scroll views. This is a pattern you encounter for example in the App Store app or in the TV app itself, or in 3rd party apps like Netflix, to just name a few examples. Whats a LazyVStack A lazy vertical stack view, or LazyVStack, is similar to VStack but only create items as they are needed. A very common pattern used to browser content in Apple TV apps is the one of a vertical list made of individually scrollable rows of content.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |