0

In my SwiftUI test app, I have this code wrapping a UIKit view in a TabView and a NavigationLink:

import SwiftUI

@main
struct NavLinkTestApp: App {
    var body: some Scene {
        WindowGroup {
            MasterView()
        }
    }
}

struct MasterView: View {
    var body: some View {
        TabView {
            RootView()
                .tabItem {
                    Label("Menu", systemImage: "list.dash")
                }
        }
    }
}

struct RootView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Detail")) {
                MyView()
            }
        }
    }
}

struct MyView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        print("makeUIView") //prints twice in iPhone 13, iOS 15.0 simulator
        
        return UIView()
    }

    func updateUIView(_ uiView: UIView, context: Context) {}
}

As noted in the code above, when I run the app in iOS 15 the makeUIView() function is called twice. In an iOS 16 simulator, makeUIView() is called only once. Also, if I remove either the TabView or NavView/NavLink then makeUIView() is called only once in either iOS 15 or iOS 16.

Should makeUIView() be called twice in the test app as coded above, or is this a bug? I suspect that this behavior is causing glitches in my production app.

protasm
  • 1,209
  • 12
  • 20
  • See [this answer](https://stackoverflow.com/q/68633861/7129318). This is more art than science. – Yrb Jun 30 '23 at 12:18

1 Answers1

0

You forgot an initial detail view, eg

        NavigationView {
            NavigationLink(destination: Text("Detail")) {
                MyView()
            }
            Text("Make a selection")
        }
malhal
  • 26,330
  • 7
  • 115
  • 133