Swift UI - HostingController adds unwanted navigation bar

Viewed 2003

I am attempting to integrate SwiftUI into my project, and I am currently using a storyboard which is launched via my app delegate with the following code:


_rootNavigiationController = [[UINavigationController alloc] init];
_rootNavigiationController.navigationBarHidden = YES;

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:StoryboardLoginRegister bundle:nil];
BasicInformation *basicInfo = (BasicInformation *)[storyboard instantiateViewControllerWithIdentifier:@"basic-info"];
[self.rootNavigiationController setViewControllers:@[basicInfo]];

So essentially my App delegate is in objective-c and the windows root controller is a UINavigation controller.

My BasicInformation class looks like:

class BasicInfo: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.isNavigationBarHidden = true;
        // Do any additional setup after loading the view.
    }
    
    @IBSegueAction func addSwiftUi(_ coder: NSCoder) -> UIViewController? {
        let BasicInfoUI = BasicInfo_UI();
        let hostingController = UIHostingController(coder: coder, rootView: BasicInfoUI);
        hostingController?.navigationController?.isNavigationBarHidden = true;
        return hostingController;
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

And the Swift UI for the basic information is the following:

struct BasicInfo_UI: View {
    @State var username: String = ""
    @State var isPrivate: Bool = true
    @State var notificationsEnabled: Bool = false
    @State private var previewIndex = 0
    var previewOptions = ["Always", "When Unlocked", "Never"]

    var body: some View {
        Form {
            Section(header: Text("PROFILE")) {
                TextField("Username", text: $username)
                Toggle(isOn: $isPrivate) {
                    Text("Private Account")
                }
            }
            
            Section(header: Text("NOTIFICATIONS")) {
                Toggle(isOn: $notificationsEnabled) {
                    Text("Enabled")
                }
                Picker(selection: $previewIndex, label: Text("Show Previews")) {
                    ForEach(0 ..< previewOptions.count) {
                        Text(self.previewOptions[$0])
                    }
                }
            }
            
            Section(header: Text("ABOUT")) {
                HStack {
                    Text("Version")
                    Spacer()
                    Text("2.2.1")
                }
            }
            
            Section {
                Button(action: {
                    print("Perform an action here...")
                }) {
                    Text("Reset All Settings")
                }
            }
        }
    }
}

struct BasicInfo_UI_Previews: PreviewProvider {
    static var previews: some View {
        BasicInfo_UI()
    }
}

My only issue is i can't seem to figure out why i have a navigation bar at the top of the UI in my app

Unwanted navigation bar

Hoping somebody can explain to me why exactly there is a navigation bar at the top of my controller event though i've explicitly set navigationbarhidden to true in multiple places in my app

2 Answers

Try to hide navigation bar by SwiftUI explicitly, like

Form {

  // ... other code

}
.navigationBarTitle("")
.navigationBarHidden(true)

After long attempts, I have the same behavior in my UIKit project and SwiftUI Views with UIHostingController as with just UIKit.

In my storyboard, the UIHostingController is embedded in the NavigationController and this in turn is connected to the UITabBarController.

The first thing to do is to uncheck "Shows Navigation Bar" in the Attributes Inspector of the NavigationController.

In the SwiftUI View I have the list in the NavigationView with the modifier .navigationBarTitle("ViewTitle", displayMode: .large) and the next SwiftUI Views without NavigationView and the list with .navigationBarTitle ("SecondViewTitle", displayMode: .inline) modifier.

enter image description here

Related