2

I have a view controller in a navigation controller. In viewDidLoad:

let button = UIButton()
button.setImage(Theme.Icon28.back.withRenderingMode(.alwaysTemplate), for: .normal)
button.addTarget(self, action: #selector(didTapBackButton), for: .touchUpInside)
button.imageEdgeInsets = Constants.buttonImageEdgeInsets
button.accessibilityIdentifier = "nav_btn_back"

navigationItem.leftBarButtonItem = button

This sets up my custom back button. Now I want to push a UIHostingController:

navigationController?.pushViewController(UIHostingController(rootView: SomeView()), animated: true)

With SomeView as:

struct SomeView: View {
    var body: some View {
        Text("Hello World")
    }
}

Now when I push the UIHostingController the back button will change back to the default system back button, the leftBarButtonItem has lost my custom button. Has anyone else run into this issue?

Why is it overriding my custom button?

Kex
  • 8,023
  • 9
  • 56
  • 129
  • you can check this [answer](https://stackoverflow.com/a/64782757/8351061) once. – Deep Gandhi May 16 '22 at 10:07
  • Every view controller has own navigation item, you change for one, then push another, by default hosting controller as it is UIViewController provides default back button. Override UIHostingController and setup custom back button for it as well. – Asperi May 16 '22 at 10:28

0 Answers0