'fullScreenCover appears only when value of isPresented changes
I have an app project on Xcode that doesn't work anymore since I upgraded to Xcode 13. On MyView(), fullScreenCover(isPresented: $myState, content: {...}) does not work when the value of myState is initialized to true before MyView() appears. In other words, when I navigate to MyView(), myState is true whereas fullScreenCover does not appear, but when I add a button that toggles myState and click it twice, fullScreenCover appears. Is there something I didn't get about fullScreenCover() ? Did something change on Xcode 13 or iOS 15 ?
struct MyView: View {
@State var myState=true
var body: some View {
VStack {
Button {
myState.toggle()
} label: {Text("toggle mustLearn")}
}
.fullScreenCover(isPresented: $myState, content: {Text("Test")})
}
}
fullScreenCover doesn't appear when I navigate to MyView() from the MainMenu():
struct MainMenu: View {
var body: some View {
NavigationView {
NavigationLink (destination: MyView(), label: {
Text("To MyView()")
})
}
}
}
Solution 1:[1]
I think its a matter of when the view is receiving the change of myState.
It works if you change from false to true onAppear like this:
struct MyView: View {
@State var myState = false
var body: some View {
VStack {
Button {
myState.toggle()
} label: {Text("toggle mustLearn")}
}
.fullScreenCover(isPresented: $myState, content: {Text("Test")})
.onAppear {
myState = true
}
}
}
struct MainMenu: View {
var body: some View {
NavigationView {
NavigationLink (destination: MyView(), label: {
Text("To MyView()")
})
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
