I started to experiment with the new SwiftUI framework, announced on the WWDC 2019 and started the tutorial on https://developer.apple.com/tutorials/swiftui.
Now I came to the point where to connect the Profile to the HomeScreen via the PresentationButton
. More precisely I am talking about this section of code in Home.swift
:
.navigationBarItems(trailing:
PresentationButton(
Image(systemName: "person.crop.circle")
.imageScale(.large)
.accessibility(label: Text("User Profile"))
.padding(),
destination: ProfileHost()
)
)
When I first click on the button the Profile Sheet appears just fine, but when I dismiss it and then click on the button again nothing happens.
Does anyone know why this is the case ?
Thanks in advance
It looks like a bug in SwiftUI.
It is probably linked to the fact that onDisappear
is never called.
You can verify that by adding
.onAppear{
print("Profile appeared")
}.onDisappear{
print("Profile disappeared")
}
to ProfileHost
view. It would make sense that an appear
should be balanced by a disappear
for the dismissal to be complete.
It is possible to work around it by implementing a function that returns a PresentationButton
that "depends" on a state variable.
@State var profilePresented: Int = 0
func profileButton(_ profilePresented: Int) -> some View {
return PresentationButton(
Image(systemName: "person.crop.circle")
.imageScale(.large)
.accessibility(label: Text("User Profile"))
.padding(),
destination: ProfileHost(),
onTrigger: {
let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
self.profilePresented += 1
})
})
}
And replace
.navigationBarItems(trailing:
PresentationButton(
Image(systemName: "person.crop.circle")
.imageScale(.large)
.accessibility(label: Text("User Profile"))
.padding(),
destination: ProfileHost()
)
)
with
.navigationBarItems(trailing: self.profileButton(self.profilePresented))
I highly recommend to not use this "solution" and just report the bug to Apple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With