Questions tagged [onappear]
42 questions
32
votes
10 answers
SwifUI onAppear gets called twice
Q1: Why are onAppears called twice?
Q2: Alternatively, where can I make my network call?
I have placed onAppears at a few different place in my code and they are all called twice. Ultimately, I'm trying to make a network call before displaying the…

AvsBest
- 435
- 1
- 4
- 9
19
votes
2 answers
SwiftUI .onAppear, only running once
In a SwiftUI app I have code like this:
var body: some View {
VStack {
Spacer()
........
}
.onAppear {
.... I want to have some code here ....
.... to run when the view appears ....
}
}
My problem is that…

Michel
- 10,303
- 17
- 82
- 179
7
votes
1 answer
SwiftUI onAppear called twice when NavigationView inside TabView
So I have a TabView where each of the tabs is embedded in a NavigationView.
On first appear of each tab I get the following lifecycle calls onAppear(), onDisappear(), onAppear(). So it looks like onAppear gets called twice. This only happens the…

andrei
- 1,353
- 15
- 24
5
votes
0 answers
Calling scrollTo() in onAppear() doesn't always work
See example code below:
struct TestView: View {
var body: some View {
ScrollViewReader { proxy in
List {
ForEach(1...30, id: \.self) { item in
Text("\(item)")
…

rayx
- 1,329
- 10
- 23
5
votes
0 answers
.onAppear not getting called when View is nested with in TabView?
The code below was working (onAppear was getting called) but as soon as I nested my two views inside a tabView (to present a UIPageViewController-like UI) the second view's onAppear function never gets called, is this a bug or something I'm doing…

GarySabo
- 5,806
- 5
- 49
- 124
3
votes
1 answer
ProgressView inside SwiftUI List disappearing after list is updated
The circular (default) SwiftUI ProgressView disappears within a List whenever the list gets expanded with new content because of a .onAppear modifier on the ProgressView.
The ProgressView becomes invisible, although its allocated cell (and its…

Thofreym
- 59
- 6
3
votes
0 answers
SwiftUI NavigationView on Apple Watch triggers multiple callings of onAppear in children views
Using Xcode Beta 12.3, the below code will call onAppear twice and print twice. When I comment out the NavigationView wrapping the ContentView, onAppear is only called once. Is this expected behavior? It seems that ANY NavigationView will trigger…

GarySabo
- 5,806
- 5
- 49
- 124
2
votes
3 answers
Why last few items does't show onAppear in normal swiftui list?
Very simple code! When i scroll down the list, show onAppear(same as task) from 0 to 24, last few items(25-29) does't show. why?
I did some logic depending on this, and succeed with old version swiftui. but now, fail in latest version.
I think maybe…

foolbear
- 726
- 1
- 7
- 19
2
votes
2 answers
SwiftUI on appear AVAudioPlayer plays multiple times
Im trying to add a AVAudioPlayer for background music on my app, I am initiating the player at the main screen, trying to start playing when the app opens but get unexpected behavior...
It plays and instantly keeps sort of creating new players and…

BlueStarXD
- 73
- 6
1
vote
1 answer
.onAppear is not called on my list items after I use the refresh control
I am trying to implement pagination on my lazyvgrid as well as a refresh control.
initially when the page loads, the pagination works - onAppear is called for each cell.
but when I pull to refresh, onAppear is not called on the cells, so only the…

Halpo
- 2,982
- 3
- 25
- 54
1
vote
1 answer
How to use On appear() with SwiftUI on iOS 16
I didn't change anything in my code, but since iOS16 the onAppear() part is not working.
selectedParcoursIndexArray.append(0) does not execute,
And app crash on :
get: { self.selectedParcoursIndexArray[num] },
Because selectedParcoursIndexArray is…

Clément Tengip
- 618
- 6
- 19
1
vote
1 answer
SwiftUI .onAppear not firing as expected when I load a new page of visible JSON data
Edited from an earlier post to include a working subset of code:
I'm apparently not understanding how .onAppear works in SwiftUI with respect to Views inside of Navigation Links. I'm trying to use it to get paged JSON (in this case from the Pokemon…

Gallaugher
- 1,593
- 16
- 27
1
vote
2 answers
SwiftUI onAppear called when TabView is released
I have a TabView with 4 tabs (page1, page2, page3, menu). When I click on any tab, onAppear is called, the problem is when I select menu and then click logout to switch from TabView to Login screen, all pages loaded before are called again (onAppear…

OuSS
- 19
- 3
1
vote
2 answers
Disable SwiftUI frame animation on appear
The Goal
Let's say I have a List or a LazyVGrid that displays multiple items nested inside a ScrollView. I use a ForEach view to generate the individual item views:
ForEach(items) { item in
ItemView(item)
}
The items array might be a @State…

Mischa
- 15,816
- 8
- 59
- 117
1
vote
4 answers
How to conditionally execute code on onAppear method
I have a swiftUi view depending on a class data.
Before displaying the data, I have to compute it in .onAppear method.
I would like to make this heavy computation only when my observed object changes.
The problem is that .onAppear is called every…

Eric
- 592
- 10
- 26