0

In SwiftUI, I have a NavigationLink destination view that has if-else inside to return different body. Like this:

    var body: some View {
        NavigationLink(destination: {
            LocalAssetView(asset: asset)
        }) {
            Color.clear
                .aspectRatio(1, contentMode: .fill)
                .overlay(
                    Image(uiImage: thumbnail)
                        .resizable()
                        .scaledToFill()
                )
        }
    }

The destination view LocalAssetView is like this:

struct LocalAssetView: View {
    let asset: PHAsset?

    var body: some View {
        if asset?.mediaType == .image {
            LocalPhotoView(asset: asset!)
        } else {
            Text("Video!")
        }
    }
}

The problem is that when the item (navigationlink) was clicked, if it's image, the destination view shows up immediately, but if it's not image, the Text("Video!") does not show up until I manually triggered another UI update (for example, switching to a different tab on screen).

It goes even stranger than that. Basically the following works:

  1. select an item that is image, the navigation link works.
  2. goes "Back".
  3. select an item that is not image, the navigation link works (i.e. show Text video).

But the following does not work:

  1. select an item that is not image, the navigation link does not show anything.
  2. then select an item that is image, the navigation link still does not show anything.

How does NavigationLink decide to update the UI or not? Is there a way to force NavigationLink always show its destination view?

XCode: 14.2 iOS target: 15.4

user1783732
  • 1,599
  • 5
  • 22
  • 44

0 Answers0