0

This will show nothing (blank screen):

import SwiftUI
import UIKit

struct ContentView: View {
    @State var navigate = false

    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("clicked"), isActive: $navigate) {
                EmptyView()
            }

            UIKitView {
                navigate = true
            }
            .navigationTitle(Text("test"))
        }
        .navigationViewStyle(.stack)
    }
}

struct UIKitView: UIViewRepresentable {
    let didClick: () -> Void

    func makeUIView(context _: Context) -> UIView {
        MyView(didClick: didClick)
    }

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

class MyView: UIView {
    let didClick: () -> Void

    init(didClick: @escaping () -> Void) {
        self.didClick = didClick

        super.init(frame: .zero)

        backgroundColor = .red

        let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))

        addGestureRecognizer(tap)
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func viewTapped(recognizer _: UIGestureRecognizer) {
        didClick()
    }
}

I am wondering where my UIKitView is. Without the NavigationLink, it is showing but than I can not navigate.

When I change the order (first NavigationLink than UIKitView), it won't navigate anymore (and the navigationTitle is gone).

How can I make this extremely simple example work without using a ZStack? This is because I use the ZStack hack (just wrap the views inside a ZStack) in my 'real' application, but that gives other problems down the line.

J. Doe
  • 12,159
  • 9
  • 60
  • 114

0 Answers0