0

I have a UIViewRepresentable that I am using the blend a project created with UIViews and SwiftUI.

My views are full screen and my issue is that I cannot find a way to pass touches through the transparent UINavigationBar at the top of the screen to my underlying UIView. That means that the top 100 or so pixels don't respond to touch even though they're visible. I have seen this post and solution, but it does not work in my case. I believe it's because I'm using UIViewRepresentable.

For my content view I have:

struct ContentView: View {
    let values: [String] = ViewType.allCases.map { "\($0.rawValue)" }
    var body: some View {
        
        NavigationView {
            List {
                ForEach(values, id: \.self) { value in
                    NavigationLink(value, destination: CustomView(viewName: value).ignoresSafeArea())
                }
                .navigationTitle("Nav Title")
            }
        }
        .accentColor(.white)
    }
}

struct CustomView: UIViewRepresentable {
    let viewType: String
    
    func makeUIView(context: Context) -> CustomView {
        CustomView(view: viewType)
    }
    
    func updateUIView(_ view: CustomView, context: Context) {
        view.draw(view.frame)
    }
}

Here is an image of my view hierarchy that shows the UINavigationBar blocking the top 100 or so pixels of my underlying UIView and preventing touches from being registered by the view.

I know I can disable the navigation bar and that does work, but I do need the back button. Ideally, I'd like to understand how I can apply the solution I linked to above which will pass touches unless there is a UIControl in the way.

Image of view hierarchy showing UINavigationBar blocking underlying UIView.

UPDATE

I tried Asperi's suggestion of using transparency and then disabling the navigation bar and it occurred to me to check the view debugger with it disabled.

It turns out that the back button is still there in the view hierarchy, but it is not visible in the app when the nav bar is disabled. If there were a way to keep the nav bar disabled but enable the button, that would be the ideal situation.

Button in debug view without UINavigationBar

rikitikitavi
  • 139
  • 13
  • Try change appearance to transparent with clear color background (this should be helpful https://stackoverflow.com/a/64074534/12299030). Alternate is to hide navigation bar for CustomView (this should be helpful https://stackoverflow.com/a/60996978/12299030). – Asperi May 04 '22 at 05:21
  • Thank you for this suggestion. I tried changing to a transparent and it doesn't affect clicking behavior. I've also successfully hidden the nav bar. Neither solution really does what I need. Ideally, I'd like to just change the shape of the UINavigationBar that spans the width of the UIView to just be a little larger than the back button but that does not seem possible. It seems I'll need to figure out a way to get my UIView to communicate with SwiftUI and create my own button. I'd like to understand why the transparent non-touchable bar is there in the first place. Its an odd design choice. – rikitikitavi May 04 '22 at 22:06

0 Answers0