Skip to content

[Bug] Adding NXNavigationBar to UIHostingController.view triggers Apple's warning #31

@JayDaniel

Description

@JayDaniel

Description

When using NXNavigationExtension with UIHostingController (SwiftUI views embedded in UIKit navigation), the console shows this warning:

[SwiftUI] Attempting to add elements to UIHostingController's view is not supported...

This happens because nx_setupNavigationBar adds nx_navigationBar directly to self.view, but for UIHostingController, self.view is a private _UIHostingView which Apple explicitly warns against modifying.

Environment

  • iOS 15+ / iOS 17+
  • Xcode 15+
  • NXNavigationExtension 4.2.6
  • Using UIHostingController subclass pushed onto UINavigationController

Steps to Reproduce

  1. Create a custom UIHostingController subclass
  2. Push it onto a UINavigationController that uses NXNavigationExtension
  3. Observe the warning in console
class MyHostingVC<Content: View>: UIHostingController<Content> {
    // ...
}

// Push to navigation stack
let vc = MyHostingVC(rootView: MySwiftUIView())
navigationController?.pushViewController(vc, animated: true)

Root Cause

In UIViewController+NXNavigationExtension.m, line ~292:

- (void)nx_setupNavigationBar {
    // ...
    if (![self.view isKindOfClass:[UIScrollView class]]) {
        [self.view addSubview:self.nx_navigationBar];  // ⚠️ Triggers warning for UIHostingController
    }
}

Since the current project architecture targets iOS 15+ and the main project heavily uses UIKit, embedding SwiftUI pages within a UIKit navigation stack is the best implementation approach for me right now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions