UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

onAppear, something i don't understand 😅

Forums > SwiftUI

Hi, There is something I must have misunderstood. I noticed something strange with onAppear. If I use it within a long List (which exceeds the screen) on the last object, the triggering of onAppear is good only when the object in question becomes visible on the screen, and not before. The desired effect works wonderfully.

Now let's say that I no longer use a list, I have a lot of elements in a VStack (which go beyond the screen), and I want to do the same, trigger onAppear on a bottom element (off screen) only when this- it becomes visible on the screen. Well it is not possible ! When I thought that Appear was used for that. What happens is that onAppear is triggered directly when the view loads, even if my elements are off screen. If someone could explain to me why and have an alternative for what I want to do, it would be wonderful :)

3      

This is by no means a technical definition of what is happening so i hope you follow. So Lists and ScrollViews are dynamic in nature. They both conform to a protocol called DynamicViewContent. Pretty much what that is saying is that the content in those views is dynamic in that it changes all the time depening on where the scroll position is. What happens if you have a list and its content exceeds the screen, any modifiers you have applied to the list will not affect any content that is off the screen until it becomes visible. This is why the onAppear wont be applied to your last object until it becomes visible.

Now a VStack is not dynamic so based on the above, any content within this view will have the modifiers applied immediately once the VStack appears. Unfortunately i dont think there is a way around this. Thinking out loud maybe if you make your VStack view dynamic this might work but im not sure how to do this. You would probably have to build your own custom VStack that is dynamic and acts like a list or scroll view, but if you are going to do that you might as well use a list or a scroll view. Another way would possibly to use GeometryReader to detect where your content is in relation to the screen and then some use use that to trigger the onAppear. Again a bit of work involved. I would suggest just sticking with List or ScrollView unless you need a fair bit of customisation based on your needs.

Can i ask why you are using a VStack with content that is off the screen? Why dont you want to use a ScrollView or List?

3      

@Newy11527 Thank you for your precise answer. My bad, I do use a scrollView, but I didn't specify it, thinking it was obvious (sorry). So I understand by reading that the scrollView is not dynamic 😞. What a pity !

For the use of GeometryReader I had thought about it and I think it is possible by this way, but I already wanted to see if something simpler did not exist before getting there, and especially understand why ! Thank you anyway.

4      

I believe you can use LazyVStack for this. Normal VStack won't work because it will load all of its child views triggering onAppear even though it is off screen.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.