SwiftUI Tutorial PresentationButton Bug

前端 未结 4 1330
生来不讨喜
生来不讨喜 2020-12-06 03:22

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 c

相关标签:
4条回答
  • 2020-12-06 03:56

    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.

    0 讨论(0)
  • 2020-12-06 04:01

    The most simple way to solve this issue is by leaving the destination: parameter on its own and have the Image object in the curly braces:

    PresentationButton(destination: ProfileHost()) {
        Image(systemName: "person.crop.circle")
            .imageScale(.large)
            .accessibility(label: Text("User Profile"))
            .padding()
    }
    
    0 讨论(0)
  • 2020-12-06 04:07

    This was a bug resolved in Xcode 11 Beta2: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes.

    With the updated API the following should work:

    PresentationButton(destination:ProfileHost()) {
        Image(systemName: "person.crop.circle")
        .imageScale(.large)
        .accessibility(label: Text("User Profile"))
        .padding()
    }
    
    0 讨论(0)
  • 2020-12-06 04:13

    This was fixed in Beta 3. I also had the same issue, where PresentationButton (now PresentationLink) was only firing once when embedded in .navigationBarItems.

    0 讨论(0)
提交回复
热议问题