I have the following app hierarchy
SceneDelegate
UIHostingController
RootView: View
UIViewControllerRepresentable
MainViewController: UITabBarController
- first tab
UINavigationController
UIHostingController<SampleContentTabView> 🟡
- second tab
UINavigationController
SampleContentAdapterController: UIViewController
UIHostingController<SampleContentTabView> 🔴
With a sample view like
struct SampleContentTabView: View {
@State var name: String = "sample"
var body: some View {
Text(name)
.navigationTitle("\(name) view")
}
}
The thing is that the first tab will correctly set the navigation title in the case of 🟡, but not in the second tab with 🔴
Is this an issue with SwiftUI?
Am I missing something?
A simplified version of the SampleContentAdapterController
would be like:
class SampleContentAdapterController: UIViewController {
let name: String
init(name: String, color: Color) {
self.name = name
super.init(nibName: nil, bundle: nil)
}
private lazy var hostingController: UIHostingController = {
UIHostingController(
rootView: SampleContentTabView(name: name)
)
}()
var hostingView: UIView { hostingController.view }
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(hostingView)
hostingView.translatesAutoresizingMaskIntoConstraints = false
hostingView.pinToSuperview()
addChild(hostingController)
hostingController.didMove(toParent: self)
}
}
A full runnable version can be found in: https://github.com/fespinoza/reproducing-bugs/tree/main/NavigationBar